home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / bfd / coffcode.h < prev    next >
Text File  |  1992-09-11  |  96KB  |  3,288 lines

  1. /* Support for Intel 960 COFF and Motorola 88k BCS COFF (and maybe others)
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*doc*
  22. @section coff backends
  23.  
  24. BFD supports a number of different flavours of coff format. The major
  25. difference between formats are the sizes and alignments of fields in
  26. structures on disk, and the occasional extra field.
  27.  
  28. Coff in all its varieties is implimented with a few common files and a
  29. number of implementation specific files. For example, The 88k bcs coff
  30. format is implemented in the file @code{m88k-bcs.c}. This file
  31. @code{#include}s @code{m88k-bcs.h} which defines the external
  32. structure of the coff format for the 88k, and @code{internalcoff.h}
  33. which defines the internal structure. @code{m88k-bcs.c} also defines
  34. the relocations used by the 88k format @xref{Relocations}. Then the
  35. major portion of coff code is included (@code{coffcode.h}) which
  36. defines the methods used to act upon the types defined in
  37. @code{m88k-bcs.h} and @code{internalcoff.h}. 
  38.  
  39. The Intel i960 processor version of coff is implemented in
  40. @code{icoff.c}. This file has the same structure as 
  41. @code{m88k-bcs.c}, except that it includes @code{intel-coff.h} rather
  42. than @code{m88k-bcs.h}. 
  43.  
  44. @subsection Porting To A New Version of Coff
  45.  
  46. The recommended method is to select from the existing implimentations
  47. the version of coff which is most like the one you want to use, for
  48. our purposes, we'll say that i386 coff is the one you select, and that
  49. your coff flavour is called foo. Copy the @code{i386coff.c} to @code{foocoff.c},
  50. copy @code{../include/i386coff.h} to @code{../include/foocoff.h} and
  51. add the lines to @code{targets.c} and @code{Makefile.in} so that your
  52. new back end is used. 
  53.  
  54. Alter the shapes of the structures in @code{../include/foocoff.h} so
  55. that they match what you need. You will probably also have to add
  56. @code{#ifdef}s to the code in @code{internalcoff.h} and
  57. @code{coffcode.h} if your version of coff is too wild.
  58.  
  59. You can verify that your new BFD backend works quite simply by
  60. building @code{objdump} from the @code{binutils} directory, and
  61. making sure that its version of what's going on at your host systems
  62. idea (assuming it has the pretty standard coff dump utility (usually
  63. called @code{att-dump} or just @code{dump})) are the same.
  64.  
  65. Then clean up your code, and send what you've done to Cygnus. Then your stuff
  66. will be in the next release, and you won't have to keep integrating
  67. it.
  68.  
  69. @subsection How The Coff Backend Works
  70.  
  71. @subsubsection Bit Twiddling
  72. Each flavour of coff supported in BFD has its own header file
  73. descibing the external layout of the structures. There is also an
  74. internal description of the coff layout (in @code{internalcoff.h})
  75. file (@code{}). A major function of the coff backend is swapping the
  76. bytes and twiddling the bits to translate the external form of the
  77. structures into the normal internal form. This is all performed in the
  78. @code{bfd_swap}_@i{thing}_@i{direction} routines. Some elements are
  79. different sizes between different versions of coff, it is the duty of
  80. the coff version specific include file to override the definitions of
  81. various packing routines in @code{coffcode.h}. Eg the size of line
  82. number entry in coff is sometimes 16 bits, and sometimes 32 bits.
  83. @code{#define}ing @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will
  84. select the correct one. No doubt, some day someone will find a version
  85. of coff which has a varying field size not catered for at the moment.
  86. To port BFD, that person will have to add more @code{#defines}.
  87.  
  88. Three of the bit twiddling routines are exported to @code{gdb};
  89. @code{coff_swap_aux_in}, @code{coff_swap_sym_in} and
  90. @code{coff_swap_linno_in}. @code{GDB} reads the symbol table on its
  91. own, but uses BFD to fix things up.
  92.  
  93. More of the bit twiddlers are exported for @code{gas};
  94. @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
  95. @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
  96. @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
  97. @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track of all
  98. the symbol table and reloc drudgery itself, thereby saving the
  99. internal BFD overhead, but uses BFD to swap things on the way out,
  100. making cross ports much safer.  This also allows BFD (and thus the
  101. linker) to use the same header files as @code{gas}, which makes one
  102. avenue to disaster disappear.
  103.  
  104. @subsubsection Symbol Reading
  105. The simple canonical form for symbols used by BFD is not rich enough
  106. to keep all the information available in a coff symbol table. The back
  107. end gets around this by keeping the original symbol table around,
  108. "behind the sceens". 
  109.  
  110. When a symbol table is requested (through a call to
  111. @code{bfd_canonicalize_symtab}, a request gets through to
  112. @code{get_normalized_symtab}. This reads the symbol table from the
  113. coff file and swaps all the structures inside into the internal form.
  114. It also fixes up all the pointers in the table (represented in the file
  115. by offsets from the first symbol in the table) into physical pointers
  116. to elements in the new internal table. This involves some work since
  117. the meanings of fields changes depending upon context; a field that is a
  118. pointer to another structure in the symbol table at one moment may be
  119. the size in bytes of a structure in the next.
  120.  
  121. Another pass is made over the table. All symbols which mark file names
  122. (@code{C_FILE} symbols) are modified so that the internal string
  123. points to the value in the auxent (the real filename) rather than the
  124. normal text associated with the symbol (@code{".file"}).
  125.  
  126. At this time the symbol names are moved around. Coff stores all
  127. symbols less than nine characters long physically within the symbol
  128. table, longer strings are kept at the end of the file in the string
  129. table. This pass moves all strings into memory, and replaces them with
  130. pointers to the strings.
  131.  
  132. The symbol table is massaged once again, this time to create the
  133. canonical table used by the BFD application. Each symbol is inspected
  134. in turn, and a decision made (using the @code{sclass} field) about the
  135. various flags to set in the @code{asymbol} @xref{Symbols}. The
  136. generated canonical table shares strings with the hidden internal
  137. symbol table.
  138.  
  139. Any linenumbers are read from the coff file too, and attached to the
  140. symbols which own the functions the linenumbers belong to.
  141.  
  142. @subsubsection Symbol Writing
  143. Writing a symbol to a coff file which didn't come from a coff file
  144. will lose any debugging information. The @code{asymbol} structure
  145. remembers the BFD from which was born, and on output the back end
  146. makes sure that the same destination target as source target is
  147. present.
  148.  
  149. When the symbols have come from a coff file then all the debugging
  150. information is preserved. 
  151.  
  152. Symbol tables are provided for writing to the back end in a vector of
  153. pointers to pointers. This allows applications like the linker to
  154. accumulate and output large symbol tables without having to do too
  155. much byte copying.
  156.  
  157. The symbol table is not output to a writable BFD until it is closed. 
  158. The order of operations on the canonical symbol table at that point
  159. are:
  160. @table @code
  161. @item coff_renumber_symbols
  162. This function runs through the provided symbol table and patches each
  163. symbol marked as a file place holder (@code{C_FILE}) to point to the
  164. next file place holder in the list. It also marks each @code{offset}
  165. field in the list with the offset from the first symbol of the current
  166. symbol. 
  167.  
  168. Another function of this procedure is to turn the canonical value form
  169. of BFD into the form used by coff. Internally, BFD expects symbol
  170. values to be offsets from a section base; so a symbol physically at
  171. 0x120, but in a section starting at 0x100, would have the value 0x20.
  172. Coff expects symbols to contain their final value, so symbols have
  173. their values changed at this point to reflect their sum with their
  174. owning section. Note that this transformation uses the
  175. @code{output_section} field of the @code{asymbol}'s @code{asection}
  176. @xref{Sections}.
  177. @item coff_mangle_symbols
  178. This routine runs though the provided symbol table and uses the
  179. offsets generated by the previous pass and the pointers generated when
  180. the symbol table was read in to create the structured hierachy
  181. required by coff. It changes each pointer to a symbol to an index into
  182. the symbol table of the symbol being referenced.
  183. @item coff_write_symbols
  184. This routine runs through the symbol table and patches up the symbols
  185. from their internal form into the coff way, calls the bit twiddlers
  186. and writes out the tabel to the file.
  187. @end table
  188. */
  189.  
  190. /*proto*
  191.  
  192. The hidden information for an asymbol is:
  193.  
  194. *+++
  195.  
  196. $ typedef struct coff_ptr_struct
  197. $ {
  198.  
  199. Remembers the offset from the first symbol in the file for this
  200. symbol. Generated by @code{coff_renumber_symbols}.
  201.  
  202. $   unsigned int offset;
  203.  
  204. Should the tag field of this symbol be renumbered.
  205. Created by @code{coff_pointerize_aux}.
  206.  
  207. $   char fix_tag;
  208.  
  209. Should the endidx field of this symbol be renumbered.
  210. Created by @code{coff_pointerize_aux}.
  211.  
  212. $   char fix_end;
  213.  
  214. The container for the symbol structure as read and translated from the file.
  215.  
  216. $   union {
  217. $     union internal_auxent auxent;
  218. $     struct internal_syment syment;
  219. $   } u;
  220. $ } combined_entry_type;
  221. $
  222.  
  223. *---
  224.  
  225. Each canonical asymbol really looks like this:
  226.  
  227. *+++
  228.  
  229. $ typedef struct coff_symbol_struct
  230. $ {
  231.  
  232. The actual symbol which the rest of BFD works with
  233.  
  234. $   asymbol symbol;
  235.  
  236. A pointer to the hidden information for this symbol
  237.  
  238. $   combined_entry_type *native;
  239.  
  240. A pointer to the linenumber information for this symbol
  241.  
  242. $   struct lineno_cache_entry *lineno;
  243. $ } coff_symbol_type;
  244.  
  245. *---
  246.  
  247. */
  248.  
  249. /* $Id: coffcode.h,v 1.27 1991/10/11 20:45:17 bothner Exp $ */
  250. /* Most of this hacked by Steve Chamberlain, steve@cygnus.com */
  251.  
  252.  
  253. #define PUTWORD bfd_h_put_32
  254. #define PUTHALF bfd_h_put_16
  255.  
  256. #ifndef GET_FCN_LNNOPTR
  257. #define GET_FCN_LNNOPTR(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  258. #endif
  259.  
  260. #ifndef GET_FCN_ENDNDX
  261. #define GET_FCN_ENDNDX(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
  262. #endif
  263.  
  264. #ifndef PUT_FCN_LNNOPTR
  265. #define PUT_FCN_LNNOPTR(abfd, in, ext)  PUTWORD(abfd,  in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  266. #endif
  267. #ifndef PUT_FCN_ENDNDX
  268. #define PUT_FCN_ENDNDX(abfd, in, ext) PUTWORD(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
  269. #endif
  270. #ifndef GET_LNSZ_LNNO
  271. #define GET_LNSZ_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_lnno)
  272. #endif
  273. #ifndef GET_LNSZ_SIZE
  274. #define GET_LNSZ_SIZE(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_size)
  275. #endif
  276. #ifndef PUT_LNSZ_LNNO
  277. #define PUT_LNSZ_LNNO(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno)
  278. #endif
  279. #ifndef PUT_LNSZ_SIZE
  280. #define PUT_LNSZ_SIZE(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size)
  281. #endif
  282. #ifndef GET_SCN_SCNLEN 
  283. #define GET_SCN_SCNLEN(abfd,  ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_scn.x_scnlen)
  284. #endif
  285. #ifndef GET_SCN_NRELOC
  286. #define GET_SCN_NRELOC(abfd,  ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nreloc)
  287. #endif
  288. #ifndef GET_SCN_NLINNO
  289. #define GET_SCN_NLINNO(abfd, ext)  bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nlinno)
  290. #endif
  291. #ifndef PUT_SCN_SCNLEN 
  292. #define PUT_SCN_SCNLEN(abfd,in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_scn.x_scnlen)
  293. #endif
  294. #ifndef PUT_SCN_NRELOC
  295. #define PUT_SCN_NRELOC(abfd,in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_scn.x_nreloc)
  296. #endif
  297. #ifndef PUT_SCN_NLINNO
  298. #define PUT_SCN_NLINNO(abfd,in, ext)  bfd_h_put_16(abfd,in, (bfd_byte *) ext->x_scn.x_nlinno)
  299. #endif
  300.  
  301.  
  302.  
  303. /* void warning(); */
  304.  
  305. /* 
  306.  * Return a word with STYP_* (scnhdr.s_flags) flags set to represent the 
  307.  * incoming SEC_* flags.  The inverse of this function is styp_to_sec_flags().
  308.  * NOTE: If you add to/change this routine, you should mirror the changes
  309.  *     in styp_to_sec_flags().
  310.  */
  311. static long 
  312. DEFUN(sec_to_styp_flags, (sec_name, sec_flags),
  313.     CONST char *        sec_name    AND
  314.     flagword    sec_flags)
  315. {
  316.     long styp_flags = 0;
  317.  
  318.     if (!strcmp(sec_name, _TEXT)) {
  319.     return((long)STYP_TEXT);
  320.     } else if (!strcmp(sec_name, _DATA)) {
  321.     return((long)STYP_DATA);
  322.     } else if (!strcmp(sec_name, _BSS)) {
  323.     return((long)STYP_BSS);
  324.     } 
  325.  
  326. /* Try and figure out what it should be */
  327.    if (sec_flags & SEC_CODE) styp_flags = STYP_TEXT; 
  328.    if (sec_flags & SEC_DATA) styp_flags = STYP_DATA; 
  329.    else if (sec_flags & SEC_READONLY) 
  330. #ifdef STYP_LIT    /* 29k readonly text/data section */
  331.        styp_flags = STYP_LIT; 
  332. #else
  333.        styp_flags = STYP_TEXT; 
  334. #endif    /* STYP_LIT */
  335.    else if (sec_flags & SEC_LOAD) styp_flags = STYP_TEXT;
  336.  
  337.    if (styp_flags == 0) styp_flags = STYP_BSS;
  338.  
  339.    return(styp_flags);
  340. }
  341. /* 
  342.  * Return a word with SEC_* flags set to represent the incoming
  343.  * STYP_* flags (from scnhdr.s_flags).   The inverse of this 
  344.  * function is sec_to_styp_flags().  
  345.  * NOTE: If you add to/change this routine, you should mirror the changes
  346.  *      in sec_to_styp_flags().
  347.  */
  348. static flagword 
  349. DEFUN(styp_to_sec_flags, (styp_flags),
  350.     long    styp_flags)
  351. {
  352.     flagword    sec_flags=0;
  353.  
  354.     if ((styp_flags & STYP_TEXT) || (styp_flags & STYP_DATA))
  355.     sec_flags = (SEC_LOAD | SEC_ALLOC);
  356.     else if (styp_flags & STYP_BSS)
  357.     sec_flags = SEC_ALLOC;
  358.  
  359. #ifdef STYP_LIT        /* A29k readonly text/data section type */
  360.     if ((styp_flags & STYP_LIT) == STYP_LIT)
  361.     sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
  362. #endif  /* STYP_LIT */
  363.  
  364.     return(sec_flags);
  365. }
  366.  
  367. #define    get_index(symbol)    ((int) (symbol)->value)
  368. #define    set_index(symbol, idx)    ((symbol)->value = (idx))
  369.  
  370. /*  **********************************************************************
  371. Here are all the routines for swapping the structures seen in the
  372. outside world into the internal forms. 
  373. */
  374.  
  375.  
  376. static void
  377. DEFUN(bfd_swap_reloc_in,(abfd, reloc_src, reloc_dst),
  378.       bfd            *abfd AND
  379.       RELOC *reloc_src AND
  380.       struct internal_reloc *reloc_dst)
  381. {
  382.   reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr);
  383.   reloc_dst->r_symndx = bfd_h_get_32(abfd, (bfd_byte *) reloc_src->r_symndx);
  384.   reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type);
  385. #if M88
  386.   reloc_dst->r_offset = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_offset);
  387. #endif
  388. }
  389.  
  390.  
  391. static unsigned int
  392. DEFUN(coff_swap_reloc_out,(abfd, src, dst),
  393.       bfd       *abfd AND
  394.       PTR    src AND
  395.       PTR    dst)
  396. {
  397.   struct internal_reloc *reloc_src = (struct internal_reloc *)src;
  398.   struct external_reloc *reloc_dst = (struct external_reloc *)dst;
  399.   bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr);
  400.   bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx);
  401.   bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *) reloc_dst->r_type);
  402. #if M88
  403.   bfd_h_put_16(abfd, reloc_src->r_offset, (bfd_byte *) reloc_dst->r_offset);
  404. #endif
  405.   return sizeof(struct external_reloc);
  406. }
  407.  
  408. static void
  409. DEFUN(bfd_swap_filehdr_in,(abfd, filehdr_src, filehdr_dst),
  410.       bfd            *abfd AND
  411.       FILHDR         *filehdr_src AND
  412.       struct internal_filehdr *filehdr_dst)
  413. {
  414.   filehdr_dst->f_magic = bfd_h_get_16(abfd, (bfd_byte *) filehdr_src->f_magic);
  415.   filehdr_dst->f_nscns = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_nscns);
  416.   filehdr_dst->f_timdat = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_timdat);
  417.   filehdr_dst->f_symptr = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_symptr);
  418.   filehdr_dst->f_nsyms = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_nsyms);
  419.   filehdr_dst->f_opthdr = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_opthdr);
  420.   filehdr_dst->f_flags = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_flags);
  421. }
  422.  
  423. static  unsigned int
  424. DEFUN(coff_swap_filehdr_out,(abfd, in, out),
  425.       bfd       *abfd AND
  426.       PTR    in AND
  427.       PTR    out)
  428. {
  429.   struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
  430.   FILHDR *filehdr_out = (FILHDR *)out;
  431.   bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
  432.   bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
  433.   bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
  434.   bfd_h_put_32(abfd, filehdr_in->f_symptr, (bfd_byte *) filehdr_out->f_symptr);
  435.   bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
  436.   bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
  437.   bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
  438.   return sizeof(FILHDR);
  439. }
  440.  
  441.  
  442. #ifndef NO_COFF_SYMBOLS
  443.  
  444. static void 
  445. DEFUN(coff_swap_sym_in,(abfd, ext1, in1),
  446.       bfd            *abfd AND
  447.       PTR ext1 AND
  448.       PTR in1)
  449. {
  450.   SYMENT *ext = (SYMENT *)ext1;
  451.   struct internal_syment      *in = (struct internal_syment *)in1;
  452.  
  453.   if( ext->e.e_name[0] == 0) {
  454.     in->_n._n_n._n_zeroes = 0;
  455.     in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
  456.   }
  457.   else {
  458. #if SYMNMLEN != E_SYMNMLEN
  459.    -> Error, we need to cope with truncating or extending SYMNMLEN!;
  460. #else
  461.     memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
  462. #endif
  463.   }
  464.   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
  465.   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
  466.   if (sizeof(ext->e_type) == 2){
  467.     in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
  468.   }
  469.   else {
  470.     in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type);
  471.   }
  472.   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
  473.   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
  474. }
  475.  
  476. static unsigned int
  477. DEFUN(coff_swap_sym_out,(abfd, inp, extp),
  478.       bfd       *abfd AND
  479.       PTR    inp AND
  480.       PTR    extp)
  481. {
  482.   struct internal_syment *in = (struct internal_syment *)inp;
  483.   SYMENT *ext =(SYMENT *)extp;
  484.   if(in->_n._n_name[0] == 0) {
  485.     bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
  486.     bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *)  ext->e.e.e_offset);
  487.   }
  488.   else {
  489. #if SYMNMLEN != E_SYMNMLEN
  490.     -> Error, we need to cope with truncating or extending SYMNMLEN!;
  491. #else
  492.     memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
  493. #endif
  494.   }
  495.   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
  496.   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
  497.   if (sizeof(ext->e_type) == 2) 
  498.       {
  499.     bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
  500.       }
  501.   else
  502.       {
  503.     bfd_h_put_32(abfd,  in->n_type , (bfd_byte *) ext->e_type);
  504.       }
  505.   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
  506.   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
  507.   return sizeof(SYMENT);
  508. }
  509.  
  510. static void
  511. DEFUN(coff_swap_aux_in,(abfd, ext1, type, class, in1),
  512.       bfd            *abfd AND
  513.       PTR           ext1 AND
  514.       int             type AND
  515.       int             class AND
  516.       PTR           in1)
  517. {
  518.   AUXENT    *ext = (AUXENT *)ext1;
  519.   union internal_auxent  *in = (union internal_auxent *)in1;
  520.   switch (class) {
  521.   case C_FILE:
  522.     if (ext->x_file.x_fname[0] == 0) {
  523.       in->x_file.x_n.x_zeroes = 0;
  524.       in->x_file.x_n.x_offset  = bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
  525.     } else {
  526. #if FILNMLEN != E_FILNMLEN
  527.    -> Error, we need to cope with truncating or extending FILNMLEN!;
  528. #else
  529.       memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
  530. #endif
  531.     }
  532.  
  533.     break;
  534.   case C_STAT:
  535. #ifdef C_LEAFSTAT
  536.   case C_LEAFSTAT:
  537. #endif
  538.   case C_HIDDEN:
  539.     if (type == T_NULL) {
  540.       in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext);
  541.       in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext);
  542.       in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext);
  543.       break;
  544.     }
  545.   default:
  546.     in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
  547. #ifndef NO_TVNDX
  548.     in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
  549. #endif
  550.  
  551.     if (ISARY(type) || class == C_BLOCK) {
  552. #if DIMNUM != E_DIMNUM
  553.    -> Error, we need to cope with truncating or extending DIMNUM!;
  554. #else
  555.       in->x_sym.x_fcnary.x_ary.x_dimen[0] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  556.       in->x_sym.x_fcnary.x_ary.x_dimen[1] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  557.       in->x_sym.x_fcnary.x_ary.x_dimen[2] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  558.       in->x_sym.x_fcnary.x_ary.x_dimen[3] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  559. #endif
  560.     }
  561.       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR(abfd, ext);
  562.       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX(abfd, ext);
  563.  
  564.     if (ISFCN(type)) {
  565.       in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
  566.     }
  567.     else {
  568.       in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext);
  569.       in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext);
  570.     }
  571.   }
  572. }
  573.  
  574. static unsigned int
  575. DEFUN(coff_swap_aux_out,(abfd, inp, type, class, extp),
  576.   bfd   *abfd AND
  577.   PTR     inp AND
  578.   int   type AND
  579.   int   class AND
  580.   PTR    extp)
  581. {
  582.   union internal_auxent *in = (union internal_auxent *)inp;
  583.   AUXENT *ext = (AUXENT *)extp;
  584.   switch (class) {
  585.   case C_FILE:
  586.     if (in->x_file.x_fname[0] == 0) {
  587.       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes );
  588.       PUTWORD(abfd,
  589.           in->x_file.x_n.x_offset,
  590.           (bfd_byte *) ext->x_file.x_n.x_offset);
  591.     }
  592.     else {
  593. #if FILNMLEN != E_FILNMLEN
  594.       -> Error, we need to cope with truncating or extending FILNMLEN!;
  595. #else
  596.       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
  597. #endif
  598.     }    
  599.     break;
  600.   case C_STAT:
  601. #ifdef C_LEAFSTAT
  602.   case C_LEAFSTAT:
  603. #endif
  604.   case C_HIDDEN:
  605.     if (type == T_NULL) {
  606.  
  607.       PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext);
  608.       PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext);
  609.       PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext);
  610.       break;
  611.     }
  612.   default:
  613.     PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
  614. #ifndef NO_TVNDX
  615.     PUTWORD(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
  616. #endif
  617.  
  618.     if (ISFCN(type)) {
  619.       PUTWORD(abfd, in->x_sym.x_misc.x_fsize, (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
  620.       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
  621.       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  622.     }
  623.     else {
  624.  
  625.       if (ISARY(type) || class == C_BLOCK) {
  626. #if DIMNUM != E_DIMNUM
  627.     -> Error, we need to cope with truncating or extending DIMNUM!;
  628. #else
  629.     bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  630.     bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  631.     bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  632.     bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  633. #endif
  634.       }
  635.       PUT_LNSZ_LNNO(abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
  636.       PUT_LNSZ_SIZE(abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
  637.  
  638.       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
  639.       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  640.  
  641.  
  642.     }
  643.   }
  644. return sizeof(AUXENT);
  645. }
  646.  
  647. #endif /* NO_COFF_SYMBOLS */
  648.  
  649. #ifndef NO_COFF_LINENOS
  650.  
  651. static void
  652. DEFUN(coff_swap_lineno_in,(abfd, ext1, in1),
  653.       bfd            *abfd AND
  654.       PTR ext1 AND
  655.       PTR in1)
  656. {
  657.   LINENO *ext = (LINENO *)ext1;
  658.   struct internal_lineno      *in = (struct internal_lineno *)in1;
  659.  
  660.   in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
  661. #if defined(M88)
  662.   in->l_lnno = bfd_h_get_32(abfd, (bfd_byte *) ext->l_lnno);
  663. #else
  664.   in->l_lnno = bfd_h_get_16(abfd, (bfd_byte *) ext->l_lnno);
  665. #endif
  666. }
  667.  
  668. static unsigned int
  669. DEFUN(coff_swap_lineno_out,(abfd, inp, outp),
  670.       bfd       *abfd AND
  671.       PTR    inp AND
  672.       PTR    outp)
  673. {
  674.   struct internal_lineno *in = (struct internal_lineno *)inp;
  675.   struct external_lineno *ext = (struct external_lineno *)outp;
  676.   PUTWORD(abfd, in->l_addr.l_symndx, (bfd_byte *) ext->l_addr.l_symndx);
  677. #if defined(M88)
  678.   PUTWORD(abfd, in->l_lnno, (bfd_byte *) ext->l_lnno);
  679. #else
  680.   PUTHALF(abfd, in->l_lnno, (bfd_byte *) ext->l_lnno);
  681. #endif
  682.   return sizeof(struct external_lineno);
  683. }
  684.  
  685. #endif /* NO_COFF_LINENOS */
  686.  
  687.  
  688. static void 
  689. DEFUN(bfd_swap_aouthdr_in,(abfd, aouthdr_ext1, aouthdr_int1),
  690.       bfd            *abfd AND
  691.       PTR aouthdr_ext1 AND
  692.       PTR aouthdr_int1)
  693. {
  694.   AOUTHDR        *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
  695.   struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
  696.  
  697.   aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic);
  698.   aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp);
  699.   aouthdr_int->tsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tsize);
  700.   aouthdr_int->dsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->dsize);
  701.   aouthdr_int->bsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->bsize);
  702.   aouthdr_int->entry = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->entry);
  703.   aouthdr_int->text_start = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->text_start);
  704.   aouthdr_int->data_start = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->data_start);
  705. #ifdef I960
  706.   aouthdr_int->tagentries = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tagentries);
  707. #endif
  708. }
  709.  
  710. static unsigned int
  711. DEFUN(coff_swap_aouthdr_out,(abfd, in, out),
  712.       bfd       *abfd AND
  713.       PTR    in AND
  714.       PTR    out)
  715. {
  716.   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
  717.   AOUTHDR *aouthdr_out = (AOUTHDR *)out;
  718.   bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->magic);
  719.   bfd_h_put_16(abfd, aouthdr_in->vstamp, (bfd_byte *) aouthdr_out->vstamp);
  720.   bfd_h_put_32(abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->tsize);
  721.   bfd_h_put_32(abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->dsize);
  722.   bfd_h_put_32(abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->bsize);
  723.   bfd_h_put_32(abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->entry);
  724.   bfd_h_put_32(abfd, aouthdr_in->text_start,
  725.            (bfd_byte *) aouthdr_out->text_start);
  726.   bfd_h_put_32(abfd, aouthdr_in->data_start, (bfd_byte *) aouthdr_out->data_start);
  727. #ifdef I960
  728.   bfd_h_put_32(abfd, aouthdr_in->tagentries, (bfd_byte *) aouthdr_out->tagentries);
  729. #endif
  730.   return sizeof(AOUTHDR);
  731. }
  732.  
  733. static void 
  734. DEFUN(coff_swap_scnhdr_in,(abfd, scnhdr_ext, scnhdr_int),
  735.       bfd            *abfd AND
  736.       SCNHDR         *scnhdr_ext AND
  737.       struct internal_scnhdr *scnhdr_int)
  738. {
  739.   memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name));
  740.   scnhdr_int->s_vaddr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_vaddr);
  741.   scnhdr_int->s_paddr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_paddr);
  742.   scnhdr_int->s_size = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_size);
  743.   scnhdr_int->s_scnptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_scnptr);
  744.   scnhdr_int->s_relptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_relptr);
  745.   scnhdr_int->s_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_lnnoptr);
  746.   scnhdr_int->s_flags = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_flags);
  747. #if defined(M88)
  748.   scnhdr_int->s_nreloc = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
  749.   scnhdr_int->s_nlnno = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
  750. #else
  751.   scnhdr_int->s_nreloc = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
  752.   scnhdr_int->s_nlnno = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
  753. #endif
  754. #ifdef I960
  755.   scnhdr_int->s_align = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_align);
  756. #endif
  757. }
  758.  
  759. static unsigned int 
  760. DEFUN(coff_swap_scnhdr_out,(abfd, in, out),
  761.       bfd       *abfd AND
  762.       PTR    in AND
  763.       PTR    out)
  764. {
  765.   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
  766.   SCNHDR *scnhdr_ext = (SCNHDR *)out;
  767.   memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
  768.   PUTWORD(abfd, scnhdr_int->s_vaddr, (bfd_byte *) scnhdr_ext->s_vaddr);
  769.   PUTWORD(abfd, scnhdr_int->s_paddr, (bfd_byte *) scnhdr_ext->s_paddr);
  770.   PUTWORD(abfd, scnhdr_int->s_size, (bfd_byte *) scnhdr_ext->s_size);
  771.   PUTWORD(abfd, scnhdr_int->s_scnptr, (bfd_byte *) scnhdr_ext->s_scnptr);
  772.   PUTWORD(abfd, scnhdr_int->s_relptr, (bfd_byte *) scnhdr_ext->s_relptr);
  773.   PUTWORD(abfd, scnhdr_int->s_lnnoptr, (bfd_byte *) scnhdr_ext->s_lnnoptr);
  774.   PUTWORD(abfd, scnhdr_int->s_flags, (bfd_byte *) scnhdr_ext->s_flags);
  775. #if defined(M88)
  776.   PUTWORD(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
  777.   PUTWORD(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
  778. #else
  779.   PUTHALF(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
  780.   PUTHALF(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
  781. #endif
  782.  
  783. #if defined(I960) 
  784.   PUTWORD(abfd, scnhdr_int->s_align, (bfd_byte *) scnhdr_ext->s_align);
  785. #endif
  786.   return sizeof(SCNHDR);
  787. }
  788.  
  789.  
  790. /*
  791.    initialize a section structure with information peculiar to this
  792.    particular implementation of coff
  793. */
  794.  
  795. static          boolean
  796. DEFUN(coff_new_section_hook,(abfd_ignore, section_ignore),
  797.       bfd            *abfd_ignore AND
  798.       asection       *section_ignore)
  799. {
  800.   section_ignore->alignment_power = abfd_ignore->xvec->align_power_min;
  801.   return true;
  802. }
  803.  
  804. /* Take a section header read from a coff file (in HOST byte order),
  805.    and make a BFD "section" out of it.  */
  806. static          boolean
  807. DEFUN(make_a_section_from_file,(abfd, hdr),
  808.       bfd            *abfd AND
  809.       struct internal_scnhdr  *hdr)
  810. {
  811.     asection       *return_section;
  812.  
  813.     {
  814.     /* Assorted wastage to null-terminate the name, thanks AT&T! */
  815.     char *name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
  816.     if (name == NULL) {
  817.         bfd_error = no_memory;
  818.         return false;
  819.     }
  820.     strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
  821.     name[sizeof (hdr->s_name)] = 0;
  822.  
  823.     return_section = bfd_make_section(abfd, name);
  824.     }
  825.  
  826.     /* s_paddr is presumed to be = to s_vaddr */
  827. #define assign(to, from) return_section->to = hdr->from
  828.     assign(vma, s_vaddr);
  829.     /* assign (vma, s_vaddr); */
  830.     assign(size, s_size);
  831.     assign(filepos, s_scnptr);
  832.     assign(rel_filepos, s_relptr);
  833.     assign(reloc_count, s_nreloc);
  834. #ifdef I960
  835.     {
  836.     /* FIXME, use a temp var rather than alignment_power */
  837.     assign(alignment_power, s_align);
  838.     {
  839.         unsigned int    i;
  840.         for (i = 0; i < 32; i++) {
  841.         if ((1 << i) >= (int) (return_section->alignment_power)) {
  842.             return_section->alignment_power = i;
  843.             break;
  844.         }
  845.         }
  846.     }
  847.     }
  848. #endif
  849.     assign(line_filepos, s_lnnoptr);
  850.     /*
  851.        return_section->linesize =   hdr->s_nlnno * sizeof (struct lineno);
  852.     */
  853.  
  854.     return_section->lineno_count = hdr->s_nlnno;
  855.     return_section->userdata = NULL;
  856.     return_section->next = (asection *) NULL;
  857.     return_section->flags = styp_to_sec_flags(hdr->s_flags);
  858.  
  859.  
  860.     if (hdr->s_nreloc != 0)
  861.     return_section->flags |= SEC_RELOC;
  862.     /* FIXME: should this check 'hdr->s_size > 0' */
  863.     if (hdr->s_scnptr != 0)
  864.     return_section->flags |= SEC_HAS_CONTENTS;
  865.     return true;
  866. }
  867. static          boolean
  868. DEFUN(coff_mkobject,(abfd),
  869.       bfd            *abfd)
  870. {
  871.   set_tdata (abfd, bfd_zalloc (abfd,sizeof(coff_data_type)));
  872.   if (coff_data(abfd) == 0) {
  873.     bfd_error = no_memory;
  874.     return false;
  875.   }
  876.   coff_data(abfd)->relocbase = 0;
  877.   return true;
  878. }
  879.  
  880. static
  881. bfd_target     *
  882. DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
  883.     bfd            *abfd AND
  884.     unsigned        nscns AND
  885.   struct internal_filehdr *internal_f AND
  886.   struct internal_aouthdr *internal_a)
  887. {
  888.   coff_data_type *coff;
  889.   enum bfd_architecture arch;
  890.   long machine;
  891.   size_t          readsize;    /* length of file_info */
  892.   SCNHDR *external_sections;
  893.   
  894.   /* Build a play area */
  895.   if (coff_mkobject(abfd) != true)
  896.     return 0;
  897.   coff = coff_data(abfd);
  898.   
  899.   
  900.   external_sections = (SCNHDR *)bfd_alloc(abfd, readsize = (nscns * SCNHSZ));
  901.   
  902.   if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
  903.     goto fail;
  904.   }
  905.   
  906.   
  907.   /* Now copy data as required; construct all asections etc */
  908.   coff->symbol_index_slew = 0;
  909.   coff->relocbase =0;
  910.   coff->raw_syment_count = 0;
  911.   coff->raw_linenos = 0;
  912.   coff->raw_syments = 0;
  913.   coff->sym_filepos =0;
  914.   coff->flags = internal_f->f_flags;
  915.   if (nscns != 0) {
  916.     unsigned int    i;
  917.     for (i = 0; i < nscns; i++) {
  918.       struct internal_scnhdr tmp;
  919.       coff_swap_scnhdr_in(abfd, external_sections + i, &tmp);
  920.       make_a_section_from_file(abfd,&tmp);
  921.     }
  922.   }
  923.   /* Determine the machine architecture and type.  */
  924. machine = 0;
  925.   switch (internal_f->f_magic) {
  926. #ifdef I386MAGIC
  927.   case I386MAGIC:
  928.     arch = bfd_arch_i386;
  929.     machine = 0;
  930.     break;
  931. #endif
  932.     
  933. #ifdef A29K_MAGIC_BIG 
  934.   case  A29K_MAGIC_BIG:
  935.   case  A29K_MAGIC_LITTLE:
  936.     arch = bfd_arch_a29k;
  937.     machine = 0;
  938.     break;
  939. #endif
  940.     
  941. #ifdef MIPS
  942.   case  MIPS_MAGIC_1:
  943.   case  MIPS_MAGIC_2:
  944.   case  MIPS_MAGIC_3:
  945.     arch = bfd_arch_mips;
  946.     machine = 0;
  947.     break;
  948. #endif
  949.     
  950. #ifdef MC68MAGIC
  951.   case MC68MAGIC:
  952.   case M68MAGIC:
  953.     arch = bfd_arch_m68k;
  954.     machine = 68020;
  955.     break;
  956. #endif
  957. #ifdef MC88MAGIC
  958.   case MC88MAGIC:
  959.   case MC88DMAGIC:
  960.   case MC88OMAGIC:
  961.     arch = bfd_arch_m88k;
  962.     machine = 88100;
  963.     break;
  964. #endif
  965. #ifdef I960
  966. #ifdef I960ROMAGIC
  967.   case I960ROMAGIC:
  968.   case I960RWMAGIC:
  969.     arch = bfd_arch_i960;
  970.     switch (F_I960TYPE & internal_f->f_flags) 
  971.     {
  972.     default:
  973.     case F_I960CORE:
  974.       machine = bfd_mach_i960_core;
  975.       break;
  976.     case F_I960KB:
  977.       machine = bfd_mach_i960_kb_sb;
  978.       break;
  979.     case  F_I960MC:
  980.       machine = bfd_mach_i960_mc;
  981.       break;
  982.     case F_I960XA:
  983.       machine = bfd_mach_i960_xa;
  984.       break;
  985.     case F_I960CA:
  986.       machine = bfd_mach_i960_ca;
  987.       break;
  988.     case F_I960KA:
  989.       machine = bfd_mach_i960_ka_sa;
  990.       break;
  991.       
  992.     }
  993.     break;
  994. #endif
  995. #endif
  996.     
  997.   default:            /* Unreadable input file type */
  998.  arch = bfd_arch_obscure;
  999.     break;
  1000.   }
  1001.   
  1002.   bfd_default_set_arch_mach(abfd, arch, machine);
  1003.   if (!(internal_f->f_flags & F_RELFLG))
  1004.     abfd->flags |= HAS_RELOC;
  1005.   if ((internal_f->f_flags & F_EXEC))
  1006.     abfd->flags |= EXEC_P;
  1007.   if (!(internal_f->f_flags & F_LNNO))
  1008.     abfd->flags |= HAS_LINENO;
  1009.   if (!(internal_f->f_flags & F_LSYMS))
  1010.     abfd->flags |= HAS_LOCALS;
  1011.   
  1012.   
  1013.   bfd_get_symcount(abfd) = internal_f->f_nsyms;
  1014.   if (internal_f->f_nsyms)
  1015.     abfd->flags |= HAS_SYMS;
  1016.   
  1017.   coff->sym_filepos = internal_f->f_symptr;
  1018.   
  1019.   /* These members communicate important constants about the symbol table
  1020.     to GDB's symbol-reading code.  These `constants' unfortunately vary
  1021.       from coff implementation to implementation...  */
  1022. #ifndef NO_COFF_SYMBOLS
  1023.   coff->local_n_btmask = N_BTMASK;
  1024.   coff->local_n_btshft = N_BTSHFT;
  1025.   coff->local_n_tmask  = N_TMASK;
  1026.   coff->local_n_tshift = N_TSHIFT;
  1027.   coff->local_symesz   = SYMESZ;
  1028.   coff->local_auxesz   = AUXESZ;
  1029.   coff->local_linesz   = LINESZ;
  1030. #endif
  1031.   
  1032.   coff->symbols = (coff_symbol_type *) NULL;
  1033.   bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0;
  1034.   
  1035.   return abfd->xvec;
  1036.  fail:
  1037.   bfd_release(abfd, coff);
  1038.   return (bfd_target *)NULL;
  1039. }
  1040.  
  1041. static bfd_target *
  1042. DEFUN(coff_object_p,(abfd),
  1043.       bfd            *abfd)
  1044. {
  1045.   int   nscns;
  1046.   FILHDR filehdr;
  1047.   AOUTHDR opthdr;
  1048.   struct internal_filehdr internal_f;
  1049.   struct internal_aouthdr internal_a;
  1050.     
  1051.   bfd_error = system_call_error;
  1052.     
  1053.   /* figure out how much to read */
  1054.   if (bfd_read((PTR) &filehdr, 1, FILHSZ, abfd) != FILHSZ)
  1055.     return 0;
  1056.     
  1057.   bfd_swap_filehdr_in(abfd, &filehdr, &internal_f);
  1058.     
  1059.   if (BADMAG(internal_f)) {
  1060.     bfd_error = wrong_format;
  1061.     return 0;
  1062.   }
  1063.   nscns =internal_f.f_nscns;
  1064.     
  1065.   if (internal_f.f_opthdr) {
  1066.     if (bfd_read((PTR) &opthdr, 1,AOUTSZ, abfd) != AOUTSZ) {
  1067.       return 0;
  1068.     }
  1069.     bfd_swap_aouthdr_in(abfd, (char *)&opthdr, (char *)&internal_a);
  1070.   }
  1071.     
  1072.   /* Seek past the opt hdr stuff */
  1073.   bfd_seek(abfd, internal_f.f_opthdr + FILHSZ, SEEK_SET);
  1074.     
  1075.   /* if the optional header is NULL or not the correct size then
  1076.      quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
  1077.      and Intel 960 readwrite headers (I960WRMAGIC) is that the
  1078.      optional header is of a different size.
  1079.      
  1080.      But the mips keeps extra stuff in it's opthdr, so dont check
  1081.      when doing that
  1082.      */
  1083.     
  1084. #if defined(M88) || defined(I960)
  1085.   if (internal_f.f_opthdr != 0 && AOUTSZ != internal_f.f_opthdr)
  1086.     return (bfd_target *)NULL;
  1087. #endif
  1088.     
  1089.   return coff_real_object_p(abfd, nscns, &internal_f, &internal_a);
  1090. }
  1091.  
  1092.  
  1093.  
  1094. #ifndef NO_COFF_LINENOS
  1095.  
  1096. static void 
  1097. DEFUN(coff_count_linenumbers,(abfd),
  1098.       bfd            *abfd)
  1099. {
  1100.   unsigned int    limit = bfd_get_symcount(abfd);
  1101.   unsigned int    i;
  1102.   asymbol       **p;
  1103.     {
  1104.       asection       *s = abfd->sections->output_section;
  1105.       while (s) {
  1106.     BFD_ASSERT(s->lineno_count == 0);
  1107.     s = s->next;
  1108.       }
  1109.     }
  1110.     
  1111.     
  1112.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
  1113.     asymbol        *q_maybe = *p;
  1114.     if (q_maybe->the_bfd->xvec->flavour == bfd_target_coff_flavour) {
  1115.       coff_symbol_type *q = coffsymbol(q_maybe);
  1116.       if (q->lineno) {
  1117.     /*
  1118.       This symbol has a linenumber, increment the owning
  1119.       section's linenumber count
  1120.       */
  1121.     alent          *l = q->lineno;
  1122.     q->symbol.section->output_section->lineno_count++;
  1123.     l++;
  1124.     while (l->line_number) {
  1125.       q->symbol.section->output_section->lineno_count++;
  1126.       l++;
  1127.     }
  1128.       }
  1129.     }
  1130.   }
  1131. }
  1132.  
  1133. #endif /* NO_COFF_LINENOS */
  1134.  
  1135. #ifndef NO_COFF_SYMBOLS
  1136.  
  1137. /* 
  1138.   Takes a bfd and a symbol, returns a pointer to the coff specific area
  1139.   of the symbol if there is one.
  1140.   */
  1141. static coff_symbol_type *
  1142. DEFUN(coff_symbol_from,(ignore_abfd, symbol),
  1143.       bfd            *ignore_abfd AND
  1144.       asymbol        *symbol)
  1145. {
  1146.   if (symbol->the_bfd->xvec->flavour != bfd_target_coff_flavour) 
  1147.     return (coff_symbol_type *)NULL;
  1148.     
  1149.   if (symbol->the_bfd->tdata == (PTR)NULL)
  1150.     return (coff_symbol_type *)NULL;
  1151.     
  1152.   return  (coff_symbol_type *) symbol;
  1153. }
  1154.  
  1155.  
  1156.  
  1157. static void
  1158. DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
  1159. coff_symbol_type *coff_symbol_ptr AND
  1160. struct internal_syment *syment)
  1161. {
  1162.  
  1163.   /* Normalize the symbol flags */
  1164.   if (coff_symbol_ptr->symbol.flags & BSF_FORT_COMM) {
  1165.     /* a common symbol is undefined with a value */
  1166.     syment->n_scnum = N_UNDEF;
  1167.     syment->n_value = coff_symbol_ptr->symbol.value;
  1168.   }
  1169.   else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
  1170.     syment->n_value = coff_symbol_ptr->symbol.value;
  1171.   }
  1172.   else if (coff_symbol_ptr->symbol.flags & BSF_UNDEFINED) {
  1173.     syment->n_scnum = N_UNDEF;
  1174.     syment->n_value = 0;
  1175.   }      
  1176.   else if (coff_symbol_ptr->symbol.flags & BSF_ABSOLUTE) {
  1177.     syment->n_scnum = N_ABS;
  1178.     syment->n_value = coff_symbol_ptr->symbol.value;
  1179.   }      
  1180.   else {
  1181.     syment->n_scnum     = 
  1182.       coff_symbol_ptr->symbol.section->output_section->index+1;
  1183.         
  1184.     syment->n_value = 
  1185.       coff_symbol_ptr->symbol.value +
  1186.     coff_symbol_ptr->symbol.section->output_offset +
  1187.       coff_symbol_ptr->symbol.section->output_section->vma;
  1188.   }
  1189. }
  1190.  
  1191. /* run through all the symbols in the symbol table and work out what
  1192.    their indexes into the symbol table will be when output 
  1193.  
  1194.  Coff requires that each C_FILE symbol points to the next one in the
  1195.  chain, and that the last one points to the first external symbol. We
  1196.  do that here too.
  1197.  
  1198. */
  1199. static void
  1200. DEFUN(coff_renumber_symbols,(bfd_ptr),
  1201.       bfd *bfd_ptr)
  1202. {
  1203.   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
  1204.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  1205.   unsigned int native_index = 0;
  1206.   struct internal_syment *last_file = (struct internal_syment *)NULL;
  1207.   unsigned int symbol_index;
  1208.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) 
  1209.       {
  1210.     coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
  1211.     if (coff_symbol_ptr && coff_symbol_ptr->native) {
  1212.       combined_entry_type *s = coff_symbol_ptr->native;
  1213.       int i;
  1214.  
  1215.       if (s->u.syment.n_sclass == C_FILE) 
  1216.           {
  1217.         if (last_file != (struct internal_syment *)NULL) {
  1218.           last_file->n_value = native_index;
  1219.         }
  1220.         last_file = &(s->u.syment);
  1221.           }
  1222.       else {
  1223.  
  1224.         /* Modify the symbol values according to their section and
  1225.            type */
  1226.  
  1227.         fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
  1228.       }
  1229.       for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
  1230.         s[i].offset = native_index ++;
  1231.       }
  1232.     }
  1233.     else {
  1234.       native_index++;
  1235.     }
  1236.       }
  1237. }
  1238.  
  1239.  
  1240. /*
  1241.  Run thorough the symbol table again, and fix it so that all pointers to
  1242.  entries are changed to the entries' index in the output symbol table.
  1243.  
  1244. */
  1245. static void 
  1246. DEFUN(coff_mangle_symbols,(bfd_ptr),
  1247.       bfd *bfd_ptr)
  1248. {
  1249.   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
  1250.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  1251.  
  1252.   unsigned int symbol_index;
  1253.  
  1254.  
  1255.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) 
  1256.       {
  1257.     coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
  1258.     if (coff_symbol_ptr && coff_symbol_ptr->native ) {
  1259.       int i;
  1260.       combined_entry_type *s = coff_symbol_ptr->native;
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.       for (i = 0; i < s->u.syment.n_numaux ; i++) {
  1267.         combined_entry_type *a = s + i + 1;
  1268.         if (a->fix_tag) {
  1269.           a->u.auxent.x_sym.x_tagndx.l = a->u.auxent.x_sym.x_tagndx.p->offset;
  1270.         }
  1271.         if (a->fix_end) {
  1272.           a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
  1273.         a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
  1274.         }
  1275.  
  1276.       }
  1277.     }
  1278.       }
  1279. }
  1280.  
  1281. #if 0
  1282.     unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
  1283.     asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  1284.     struct internal_syment *last_tagndx = (struct internal_syment *)NULL;
  1285.     struct internal_syment *last_file = (struct internal_syment *)NULL;
  1286.     struct internal_syment *last_fcn = (struct internal_syment *)NULL;
  1287.     struct internal_syment *block_stack[50];
  1288.     struct internal_syment **last_block = &block_stack[0];
  1289.     boolean first_time = true;  
  1290.     unsigned int symbol_index;
  1291.     unsigned int native_index = 0;
  1292.     
  1293.     for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) {
  1294.       coff_symbol_type *coff_symbol_ptr =
  1295.     coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
  1296.       if (coff_symbol_ptr == (coff_symbol_type *)NULL) {
  1297.     /* 
  1298.       This symbol has no coff information in it, it will take up
  1299.         only one slot in the output symbol table
  1300.           */
  1301.     native_index++;
  1302.       }
  1303.       else {
  1304.     struct internal_syment *syment = coff_symbol_ptr->native;
  1305.     if (syment == (struct internal_syment *)NULL) {
  1306.       native_index++;
  1307.     }
  1308.     else {
  1309.       /* Normalize the symbol flags */
  1310.       if (coff_symbol_ptr->symbol.flags & BSF_FORT_COMM) {
  1311.         /* a common symbol is undefined with a value */
  1312.         syment->n_scnum = N_UNDEF;
  1313.         syment->n_value = coff_symbol_ptr->symbol.value;
  1314.       }
  1315.       else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
  1316.         syment->n_value = coff_symbol_ptr->symbol.value;
  1317.       }
  1318.       else if (coff_symbol_ptr->symbol.flags & BSF_UNDEFINED) {
  1319.         syment->n_scnum = N_UNDEF;
  1320.         syment->n_value = 0;
  1321.       }      
  1322.       else if (coff_symbol_ptr->symbol.flags & BSF_ABSOLUTE) {
  1323.         syment->n_scnum = N_ABS;
  1324.         syment->n_value = coff_symbol_ptr->symbol.value;
  1325.       }      
  1326.       else {
  1327.         syment->n_scnum     = 
  1328.           coff_symbol_ptr->symbol.section->output_section->index+1;
  1329.         
  1330.         syment->n_value = 
  1331.           coff_symbol_ptr->symbol.value +
  1332.         coff_symbol_ptr->symbol.section->output_offset +
  1333.           coff_symbol_ptr->symbol.section->output_section->vma;
  1334.       }
  1335.       
  1336.       
  1337.       /* If this symbol ties up something then do it */
  1338.       
  1339.       if (syment->n_sclass == C_FILE && last_file != (struct internal_syment *)NULL)
  1340.           {
  1341.         last_file->n_value = native_index;
  1342.           }
  1343.       else if ((syment->n_sclass == C_EXT 
  1344.             || syment->n_sclass == C_STAT 
  1345. #ifdef C_LEAFEXT
  1346.             || syment->n_sclass == C_LEAFEXT 
  1347.               || syment->n_sclass == C_LEAFSTAT
  1348. #endif
  1349.           )
  1350.          && last_fcn != (struct internal_syment *)NULL) 
  1351.         {
  1352.           union internal_auxent *auxent = (union internal_auxent *)(last_fcn+1);
  1353.           auxent->x_sym.x_fcnary.x_fcn.x_endndx.l = native_index;
  1354.           last_fcn = (struct internal_syment *)NULL;
  1355.           
  1356.         }
  1357.     else if (syment->n_sclass == C_EOS && last_tagndx != (struct internal_syment*)NULL)
  1358.         {
  1359.           union internal_auxent *auxent = (union internal_auxent *)(last_tagndx+1);
  1360.           /* Remember that we keep the native index in the offset 
  1361.          so patch the beginning of the struct to point to this
  1362.          */
  1363. /*if (last_
  1364.           auxent->x_sym.x_tagndx =    last_tagndx->_n._n_n._n_offset;*/
  1365.           auxent->x_sym.x_fcnary.x_fcn.x_endndx.l = syment->n_numaux + 1 + native_index;
  1366.           /* Now point the eos to the structure */
  1367.           auxent = (union internal_auxent *)(syment+1);
  1368.           auxent->x_sym.x_tagndx.l =  last_tagndx->_n._n_n._n_offset;
  1369.         }
  1370.     else if (syment->n_sclass == C_BLOCK 
  1371.          && coff_symbol_ptr->symbol.name[1] == 'e') 
  1372.         {
  1373.           union internal_auxent *auxent = (union internal_auxent *)((*(--last_block))+1);
  1374.           auxent->x_sym.x_fcnary.x_fcn.x_endndx.l = native_index + syment->n_numaux + 1;
  1375.         }
  1376.     if (syment->n_sclass == C_EXT 
  1377.         && !ISFCN(syment->n_type) 
  1378.         && first_time == true 
  1379.         && last_file != (struct internal_syment *)NULL) {
  1380.       /* This is the first external symbol seen which isn't a 
  1381.          function place it in the last .file entry */
  1382.       last_file->n_value = native_index;
  1383.       first_time = false;
  1384.     }
  1385. #ifdef C_LEAFPROC
  1386.     if (syment->n_sclass == C_LEAFPROC &&
  1387.         syment->n_numaux == 2) {
  1388.       union internal_auxent *auxent = (union internal_auxent *)(syment+2);
  1389.       /* This is the definition of a leaf proc, we'll relocate the 
  1390.          address */
  1391.       auxent->x_bal.x_balntry =       
  1392.         coff_symbol_ptr->symbol.section->output_offset + 
  1393.           coff_symbol_ptr->symbol.section->output_section->vma +
  1394.         auxent->x_bal.x_balntry   ;
  1395.     }
  1396. #endif
  1397.     /* If this symbol needs to be tied up then remember some facts */
  1398.     if (syment->n_sclass == C_FILE) 
  1399.         {
  1400.           last_file = syment;
  1401.         }
  1402.     if (syment->n_numaux != 0) {
  1403.       /*
  1404.         If this symbol would like to point to something in the
  1405.         future then remember where it is 
  1406.         */
  1407.       if (uses_x_sym_x_tagndx_p(bfd_ptr, syment)) {
  1408.         /* 
  1409.           If this is a ref to a structure then we'll tie it up 
  1410.           now - there are never any forward refs for one 
  1411.           */
  1412.         if (syment->n_sclass == C_STRTAG ||
  1413.         syment->n_sclass == C_ENTAG ||
  1414.         syment->n_sclass == C_UNTAG) {
  1415.           last_tagndx = syment;
  1416.         }
  1417.         else {
  1418.           /*
  1419.         This is a ref to a structure - the structure must
  1420.         have been defined within the same file, and previous
  1421.         to this point, so we can deduce the new tagndx
  1422.         directly.
  1423.         */
  1424.           union internal_auxent *auxent = (union internal_auxent *)(syment+1);
  1425.           bfd *bfd_ptr = coff_symbol_ptr->symbol.the_bfd;
  1426.           struct internal_syment *base = obj_raw_syments(bfd_ptr);          
  1427. /*          auxent->x_sym.x_tagndx = base[auxent->x_sym.x_tagndx]._n._n_n._n_offset;*/
  1428.           
  1429.           
  1430.         }
  1431.       }
  1432.       if (ISFCN(syment->n_type)) {
  1433.         last_fcn = syment;
  1434.       }
  1435.       if (syment->n_sclass == C_BLOCK 
  1436.           && coff_symbol_ptr->symbol.name[1] == 'b')
  1437.           {
  1438.         *last_block++ = syment;
  1439.           }
  1440.     }
  1441.     syment->_n._n_n._n_offset = native_index;
  1442.     native_index = native_index + 1 + syment->n_numaux;
  1443.       }
  1444.     }
  1445.   }
  1446. }
  1447.  
  1448.  
  1449. #endif
  1450. static int string_size; 
  1451. static void
  1452. DEFUN(coff_fix_symbol_name,(ignore_abfd, symbol, native),
  1453.   bfd *ignore_abfd AND
  1454.   asymbol *symbol AND
  1455.   combined_entry_type *native)
  1456. {
  1457.   unsigned int    name_length;
  1458.   union internal_auxent *auxent;
  1459.   char *  name = ( char *)(symbol->name);
  1460.  
  1461.   if (name == (char *) NULL) {
  1462.     /* coff symbols always have names, so we'll make one up */
  1463.     symbol->name = "strange";
  1464.     name = (char *)symbol->name;
  1465.   }
  1466.   name_length = strlen(name);
  1467.           
  1468.   if (native->u.syment.n_sclass == C_FILE) {
  1469.     strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
  1470.     auxent = &(native+1)->u.auxent;
  1471.         
  1472. #ifdef COFF_LONG_FILENAMES
  1473.     if (name_length <= FILNMLEN) {
  1474.       strncpy(auxent->x_file.x_fname, name, FILNMLEN);
  1475.     }
  1476.     else {
  1477.       auxent->x_file.x_n.x_offset = string_size + 4;
  1478.       auxent->x_file.x_n.x_zeroes = 0;
  1479.       string_size += name_length + 1;
  1480.     }
  1481. #else
  1482.     strncpy(auxent->x_file.x_fname, name, FILNMLEN);
  1483.     if (name_length > FILNMLEN) {
  1484.       name[FILNMLEN] = '\0';
  1485.     }
  1486. #endif
  1487.   }
  1488.   else
  1489.       {                /* NOT A C_FILE SYMBOL */
  1490.     if (name_length <= SYMNMLEN) {
  1491.       /* This name will fit into the symbol neatly */
  1492.       strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
  1493.     }
  1494.     else {
  1495.       native->u.syment._n._n_n._n_offset =  string_size + 4;
  1496.       native->u.syment._n._n_n._n_zeroes = 0;
  1497.       string_size += name_length + 1;
  1498.     }
  1499.       }
  1500. }
  1501.  
  1502.  
  1503.  
  1504. static unsigned int 
  1505. DEFUN(coff_write_symbol,(abfd, symbol, native, written),
  1506. bfd *abfd AND
  1507. asymbol *symbol AND
  1508. combined_entry_type *native AND
  1509. unsigned int written)
  1510. {
  1511.   unsigned int    numaux = native->u.syment.n_numaux;
  1512.   int             type = native->u.syment.n_type;
  1513.   int             class =  native->u.syment.n_sclass;
  1514.   SYMENT buf;
  1515.   unsigned int j;
  1516.  
  1517.   coff_fix_symbol_name(abfd, symbol, native);
  1518.   coff_swap_sym_out(abfd, &native->u.syment, &buf);
  1519.   bfd_write((PTR)& buf, 1, SYMESZ, abfd);
  1520.   for (j = 0; j != native->u.syment.n_numaux;  j++) 
  1521.       {
  1522.     AUXENT buf1;
  1523.     coff_swap_aux_out(abfd,
  1524.              &( (native + j + 1)->u.auxent), type, class, &buf1);
  1525.     bfd_write((PTR) (&buf1), 1, AUXESZ, abfd);
  1526.       }
  1527.   /*
  1528.     Reuse somewhere in the symbol to keep the index
  1529.     */
  1530.   set_index(symbol, written);
  1531.   return   written + 1 + numaux;
  1532. }
  1533.  
  1534.  
  1535. static unsigned int
  1536. DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
  1537.       bfd *abfd AND
  1538.       asymbol *symbol AND
  1539.       unsigned int written)
  1540. {
  1541.   /*
  1542.     This symbol has been created by the loader, or come from a non
  1543.     coff format. It  has no native element to inherit, make our
  1544.     own
  1545.     */
  1546.  combined_entry_type *native;
  1547.  combined_entry_type dummy;
  1548.   native = &dummy;
  1549.   native->u.syment.n_type =  T_NULL;
  1550. #ifdef I960
  1551.   native->u.syment.n_flags =  0;
  1552. #endif
  1553.   if (symbol->flags & BSF_ABSOLUTE) {
  1554.     native->u.syment.n_scnum  =  N_ABS;
  1555.     native->u.syment.n_value =  symbol->value;
  1556.   }
  1557.   else if (symbol->flags & (BSF_UNDEFINED | BSF_FORT_COMM)) {
  1558.     native->u.syment.n_scnum =  N_UNDEF;
  1559.     native->u.syment.n_value =  symbol->value;
  1560.   }
  1561.   else if (symbol->flags & BSF_DEBUGGING) {
  1562.     /*
  1563.       remove name so it doesn't take up any space
  1564.       */
  1565.     symbol->name = "";
  1566.   }
  1567.   else {
  1568.     native->u.syment.n_scnum  =   symbol->section->output_section->index +
  1569.       1;
  1570.     native->u.syment.n_value =   symbol->value +
  1571.       symbol->section->output_section->vma +
  1572.     symbol->section->output_offset;
  1573. #ifdef I960
  1574.     /* Copy the any flags from the the file hdr into the symbol  */
  1575.       {
  1576.     coff_symbol_type *c = coff_symbol_from(abfd, symbol);
  1577.     if (c != (coff_symbol_type *)NULL) {
  1578.       native->u.syment.n_flags =   c->symbol.the_bfd->flags;
  1579.     }
  1580.       }
  1581. #endif
  1582.   }
  1583.   
  1584. #ifdef HASPAD1
  1585.   native->u.syment.pad1[0] = 0;
  1586.   native->u.syment.pad1[0] = 0;
  1587. #endif
  1588.   
  1589.   native->u.syment.n_type =  0;
  1590.   if (symbol->flags & BSF_LOCAL)
  1591.     native->u.syment.n_sclass =  C_STAT;
  1592.   else 
  1593.     native->u.syment.n_sclass =  C_EXT;
  1594.   native->u.syment.n_numaux =  0;
  1595.  
  1596.   return   coff_write_symbol(abfd, symbol, native, written);
  1597. }
  1598.  
  1599. static unsigned int 
  1600. DEFUN(coff_write_native_symbol,(abfd, symbol,   written),
  1601. bfd *abfd AND
  1602. coff_symbol_type *symbol AND
  1603. unsigned int written)
  1604. {
  1605.   /*
  1606.     Does this symbol have an ascociated line number - if so then
  1607.     make it remember this symbol index. Also tag the auxent of
  1608.     this symbol to point to the right place in the lineno table
  1609.     */
  1610.   combined_entry_type *native = symbol->native;
  1611.  
  1612.   alent          *lineno = symbol->lineno;
  1613.  
  1614.   if (lineno) {
  1615.     unsigned int    count = 0;
  1616.     lineno[count].u.offset = written;
  1617.     if (native->u.syment.n_numaux) {
  1618.       union internal_auxent  *a = &((native+1)->u.auxent);
  1619.             
  1620.       a->x_sym.x_fcnary.x_fcn.x_lnnoptr =  
  1621.     symbol->symbol.section->output_section->moving_line_filepos;
  1622.     }
  1623.     /*
  1624.       And count and relocate all other linenumbers
  1625.       */
  1626.     count++;
  1627.     while (lineno[count].line_number) {
  1628.       lineno[count].u.offset +=
  1629.     symbol->symbol.section->output_section->vma +
  1630.       symbol->symbol.section->output_offset;
  1631.       count++;
  1632.     }
  1633.     symbol->symbol.section->output_section->moving_line_filepos +=
  1634.       count * LINESZ;
  1635.           
  1636.   }
  1637.   return coff_write_symbol(abfd, &( symbol->symbol), native,written);
  1638. }
  1639.  
  1640. static void 
  1641. DEFUN(coff_write_symbols,(abfd),
  1642.       bfd            *abfd)
  1643. {
  1644.   unsigned int    i;
  1645.   unsigned int    limit = bfd_get_symcount(abfd);
  1646.   unsigned int    written = 0;
  1647.  
  1648.   asymbol       **p;
  1649.  
  1650.   string_size = 0;
  1651.     
  1652.     
  1653.   /* Seek to the right place */
  1654.   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
  1655.     
  1656.   /* Output all the symbols we have */
  1657.     
  1658.   written = 0;
  1659.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) 
  1660.       {
  1661.     asymbol        *symbol = *p;
  1662.     coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
  1663.       
  1664.  
  1665.  
  1666.     if (c_symbol == (coff_symbol_type *) NULL ||
  1667.         c_symbol->native == (combined_entry_type *)NULL)
  1668.         {
  1669.           written = coff_write_alien_symbol(abfd, symbol, written);
  1670.         }
  1671.     else
  1672.         {
  1673.           written = coff_write_native_symbol(abfd, c_symbol, written);
  1674.         }
  1675.  
  1676.       }
  1677.  
  1678.   bfd_get_symcount(abfd) = written;
  1679.  
  1680.   /* Now write out strings */
  1681.     
  1682.   if (string_size != 0) 
  1683.    {
  1684.      unsigned int    size = string_size + 4;
  1685.      bfd_byte buffer[4];
  1686.  
  1687.      bfd_h_put_32(abfd, size, buffer);
  1688.      bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
  1689.      for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) 
  1690.      {
  1691.        asymbol        *q = *p;
  1692.        size_t          name_length = strlen(q->name);
  1693.        int maxlen;
  1694.        coff_symbol_type*       c_symbol = coff_symbol_from(abfd, q);
  1695.        maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
  1696.              (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
  1697.          FILNMLEN : SYMNMLEN;
  1698.     
  1699.        if (name_length > maxlen) {
  1700.          bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
  1701.        }
  1702.      }
  1703.    }
  1704.   else {
  1705.     /* We would normally not write anything here, but we'll write
  1706.        out 4 so that any stupid coff reader which tries to read
  1707.        the string table even when there isn't one won't croak.
  1708.        */
  1709.       
  1710.     uint32e_type size = 4;
  1711.     size =  size;
  1712.     bfd_write((PTR)&size, 1, sizeof(size), abfd);
  1713.       
  1714.   }
  1715. }
  1716.  
  1717. /*doc*
  1718. @subsubsection Writing Relocations
  1719. To write a relocations, all the back end does is step though the
  1720. canonical relocation table, and create an @code{internal_reloc}. The
  1721. symbol index to use is removed from the @code{offset} field in the
  1722. symbol table supplied, the address comes directly from the sum of the
  1723. section base address and the relocation offset and the type is dug
  1724. directly from the howto field.
  1725.  
  1726. Then the @code{internal_reloc} is swapped into the shape of an
  1727. @code{external_reloc} and written out to disk.
  1728. */
  1729.  
  1730. static void 
  1731. DEFUN(coff_write_relocs,(abfd),
  1732.       bfd            *abfd)
  1733. {
  1734.   asection       *s;
  1735.   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
  1736.     unsigned int    i;
  1737.     struct external_reloc dst;
  1738.       
  1739.     arelent       **p = s->orelocation;
  1740.     bfd_seek(abfd, s->rel_filepos, SEEK_SET);
  1741.     for (i = 0; i < s->reloc_count; i++) {
  1742.       struct internal_reloc    n;
  1743.       arelent        *q = p[i];
  1744.       memset((PTR)&n, 0, sizeof(n));
  1745.       n.r_vaddr = q->address + s->vma;
  1746.       if (q->sym_ptr_ptr) {
  1747.     n.r_symndx = get_index((*(q->sym_ptr_ptr)));
  1748.       }
  1749. #ifdef SELECT_RELOC
  1750.       /* Work out reloc type from what is required */
  1751.       SELECT_RELOC(n.r_type, q->howto);
  1752. #else
  1753.       n.r_type = q->howto->type;
  1754. #endif
  1755.       coff_swap_reloc_out(abfd, &n, &dst);
  1756.       bfd_write((PTR) &n, 1, RELSZ, abfd);
  1757.     }
  1758.   }
  1759. }
  1760. #endif /* NO_COFF_SYMBOLS */
  1761.  
  1762. #ifndef NO_COFF_LINENOS
  1763.  
  1764. static void 
  1765. DEFUN(coff_write_linenumbers,(abfd),
  1766.       bfd            *abfd)
  1767. {
  1768.   asection       *s;
  1769.   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
  1770.     if (s->lineno_count) {
  1771.       asymbol       **q = abfd->outsymbols;
  1772.       bfd_seek(abfd, s->line_filepos, SEEK_SET);
  1773.       /* Find all the linenumbers in this section */
  1774.       while (*q) {
  1775.     asymbol        *p = *q;
  1776.     alent          *l = BFD_SEND(p->the_bfd, _get_lineno, (p->the_bfd, p));
  1777.     if (l) {
  1778.       /* Found a linenumber entry, output */
  1779.       struct internal_lineno  out;
  1780.       LINENO buff;
  1781.       memset( (PTR)&out, 0, sizeof(out));
  1782.       out.l_lnno = 0;
  1783.       out.l_addr.l_symndx = l->u.offset;
  1784.       coff_swap_lineno_out(abfd, &out, &buff);
  1785.       bfd_write((PTR) &buff, 1, LINESZ, abfd);
  1786.       l++;
  1787.       while (l->line_number) {
  1788.         out.l_lnno = l->line_number;
  1789.         out.l_addr.l_symndx = l->u.offset;
  1790.         coff_swap_lineno_out(abfd, &out, &buff);
  1791.         bfd_write((PTR) &buff, 1, LINESZ, abfd);
  1792.         l++;
  1793.       }
  1794.     }
  1795.     q++;
  1796.       }
  1797.     }
  1798.   }
  1799. }
  1800.  
  1801. static alent   *
  1802. DEFUN(coff_get_lineno,(ignore_abfd, symbol),
  1803.       bfd            *ignore_abfd AND
  1804.       asymbol        *symbol)
  1805. {
  1806.   return coffsymbol(symbol)->lineno;
  1807. }
  1808.  
  1809. #endif /* NO_COFF_LINENOS */
  1810.  
  1811. static asymbol *
  1812. coff_make_empty_symbol(abfd)
  1813. bfd            *abfd;
  1814. {
  1815.   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
  1816.   if (new == NULL) {
  1817.     bfd_error = no_memory;
  1818.     return (NULL);
  1819.   }                /* on error */
  1820.   new->native = 0;
  1821.   new->lineno = (alent *) NULL;
  1822.   new->symbol.the_bfd = abfd;
  1823.   return &new->symbol;
  1824. }
  1825.  
  1826. #ifndef NO_COFF_SYMBOLS
  1827.  
  1828. static void 
  1829. DEFUN(coff_print_symbol,(ignore_abfd, filep, symbol, how),
  1830.       bfd            *ignore_abfd AND
  1831.       PTR           filep AND
  1832.       asymbol        *symbol AND
  1833.       bfd_print_symbol_type how)
  1834. {
  1835.   FILE *file = (FILE *)filep;
  1836.   switch (how) {
  1837.   case bfd_print_symbol_name:
  1838.     fprintf(file, "%s", symbol->name);
  1839.     break;
  1840.   case bfd_print_symbol_more:
  1841.     fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
  1842.         (unsigned long) coffsymbol(symbol)->lineno);
  1843.     break;
  1844.   case bfd_print_symbol_all:
  1845.       {
  1846.     CONST char           *section_name = symbol->section == (asection *) NULL ?
  1847.       "*abs" : symbol->section->name;
  1848.     bfd_print_symbol_vandf((PTR) file, symbol);
  1849.       
  1850.     fprintf(file, " %-5s %s %s %s",
  1851.         section_name,
  1852.         coffsymbol(symbol)->native ? "n" : "g",
  1853.         coffsymbol(symbol)->lineno ? "l" : " ",
  1854.         symbol->name);
  1855.       }
  1856.       
  1857.       
  1858.     break;
  1859.   }
  1860. }
  1861.  
  1862. #endif /* NO_COFF_SYMBOLS */
  1863.  
  1864. /* Set flags and magic number of a coff file from architecture and machine
  1865.    type.  Result is true if we can represent the arch&type, false if not.  */
  1866.  
  1867. static          boolean
  1868. DEFUN(coff_set_flags,(abfd, magicp, flagsp),
  1869.       bfd            *abfd AND
  1870.       unsigned       *magicp AND
  1871.       unsigned short *flagsp)
  1872. {
  1873.   switch (bfd_get_arch(abfd)) {
  1874.       
  1875. #ifdef I960ROMAGIC
  1876.       
  1877.   case bfd_arch_i960:
  1878.       
  1879.       {
  1880.     unsigned        flags;
  1881.     *magicp = I960ROMAGIC;
  1882.     /*
  1883.       ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
  1884.       I960RWMAGIC);   FIXME???
  1885.       */
  1886.     switch (bfd_get_mach(abfd)) {
  1887.     case bfd_mach_i960_core:
  1888.       flags = F_I960CORE;
  1889.       break;
  1890.     case bfd_mach_i960_kb_sb:
  1891.       flags = F_I960KB;
  1892.       break;
  1893.     case bfd_mach_i960_mc:
  1894.       flags = F_I960MC;
  1895.       break;
  1896.     case bfd_mach_i960_xa:
  1897.       flags = F_I960XA;
  1898.       break;
  1899.     case bfd_mach_i960_ca:
  1900.       flags = F_I960CA;
  1901.       break;
  1902.     case bfd_mach_i960_ka_sa:
  1903.       flags = F_I960KA;
  1904.       break;
  1905.     default:
  1906.       return false;
  1907.     }
  1908.     *flagsp = flags;
  1909.     return true;
  1910.       }
  1911.     break;
  1912. #endif
  1913. #ifdef MIPS
  1914.   case bfd_arch_mips:
  1915.     *magicp = MIPS_MAGIC_2;
  1916.     return true;
  1917.     break;
  1918. #endif
  1919. #ifdef I386MAGIC
  1920.   case bfd_arch_i386:
  1921.     *magicp = I386MAGIC;
  1922.     return true;
  1923. #endif
  1924. #ifdef MC68MAGIC
  1925.   case bfd_arch_m68k:
  1926.     *magicp = MC68MAGIC;
  1927.     return true;
  1928. #endif
  1929.     
  1930. #ifdef MC88MAGIC
  1931.   case bfd_arch_m88k:
  1932.     *magicp = MC88OMAGIC;
  1933.     return true;
  1934.     break;
  1935. #endif
  1936.  
  1937. #ifdef A29K_MAGIC_BIG
  1938.   case bfd_arch_a29k:
  1939.     if (abfd->xvec->byteorder_big_p)
  1940.         *magicp = A29K_MAGIC_BIG;
  1941.     else
  1942.         *magicp = A29K_MAGIC_LITTLE;
  1943.     return true;
  1944.     break;
  1945. #endif
  1946.     
  1947.   default:            /* Unknown architecture */
  1948.     /* return false;  -- fall through to "return false" below, to avoid
  1949.        "statement never reached" errors on the one below. */    
  1950.     break;
  1951.   }
  1952.     
  1953.   return false;
  1954. }
  1955.  
  1956.  
  1957. static          boolean
  1958. DEFUN(coff_set_arch_mach,(abfd, arch, machine),
  1959.       bfd            *abfd AND
  1960.       enum bfd_architecture arch AND
  1961.       unsigned long   machine)
  1962. {
  1963.   unsigned        dummy1;
  1964.   unsigned     short dummy2;
  1965.   bfd_default_set_arch_mach(abfd, arch, machine);
  1966.  
  1967.   if (arch != bfd_arch_unknown &&
  1968.       coff_set_flags(abfd, &dummy1, &dummy2) != true)
  1969.     return false;        /* We can't represent this type */
  1970.   return true;            /* We're easy ... */
  1971. }
  1972.  
  1973.  
  1974. /* Calculate the file position for each section. */
  1975.  
  1976. static void 
  1977. DEFUN(coff_compute_section_file_positions,(abfd),
  1978.       bfd            *abfd)
  1979. {
  1980.   asection       *current;
  1981.   file_ptr        sofar = FILHSZ;
  1982.   if (bfd_get_start_address(abfd)) {
  1983.     /*
  1984.       A start address may have been added to the original file. In this
  1985.       case it will need an optional header to record it.
  1986.       */
  1987.     abfd->flags |= EXEC_P;
  1988.   }
  1989.   if (abfd->flags & EXEC_P)
  1990.     sofar += AOUTSZ;
  1991.   
  1992.   
  1993.   sofar += abfd->section_count * SCNHSZ;
  1994.   for (current = abfd->sections;    
  1995.        current != (asection *)NULL;
  1996.        current = current->next) {
  1997.     /* Only deal with sections which have contents */
  1998.     if (!(current->flags & SEC_HAS_CONTENTS))
  1999.       continue;
  2000.     
  2001.     /* Align the sections in the file to the same boundary on 
  2002.        which they are aligned in virtual memory.  I960 doesn't
  2003.        do this (FIXME) so we can stay in sync with Intel.  960
  2004.        doesn't yet page from files... */
  2005. #ifndef I960
  2006.     sofar = ALIGN(sofar, 1 << current->alignment_power);
  2007. #endif
  2008.     /* FIXME, in demand paged files, the low order bits of the file
  2009.        offset must match the low order bits of the virtual address.
  2010.        "Low order" is apparently implementation defined.  Add code
  2011.        here to round sofar up to match the virtual address.  */
  2012.     
  2013.     current->filepos = sofar;
  2014.     sofar += current->size;
  2015.   }
  2016.   obj_relocbase(abfd) = sofar;
  2017. }
  2018.  
  2019.  
  2020.  
  2021.  
  2022. /* SUPPRESS 558 */
  2023. /* SUPPRESS 529 */
  2024. static          boolean
  2025. DEFUN(coff_write_object_contents,(abfd),
  2026.       bfd            *abfd)
  2027.   {
  2028.     asection       *current;
  2029.     boolean         hasrelocs = false;
  2030.     boolean         haslinno = false;
  2031.     file_ptr        reloc_base;
  2032.     file_ptr        lineno_base;
  2033.     file_ptr        sym_base;
  2034.     file_ptr        scn_base;
  2035.     file_ptr        data_base;
  2036.     unsigned long   reloc_size = 0;
  2037.     unsigned long   lnno_size = 0;
  2038.     asection       *text_sec = NULL;
  2039.     asection       *data_sec = NULL;
  2040.     asection       *bss_sec = NULL;
  2041.     
  2042.     struct internal_filehdr internal_f;
  2043.     struct internal_aouthdr internal_a;
  2044.     
  2045.     
  2046.     bfd_error = system_call_error;
  2047.     
  2048.     
  2049.     if(abfd->output_has_begun == false) {
  2050.       coff_compute_section_file_positions(abfd);
  2051.     }
  2052.     
  2053.     if (abfd->sections != (asection *)NULL) {
  2054.       scn_base = abfd->sections->filepos;
  2055.   }
  2056.   else {
  2057.     scn_base = 0;
  2058.   }
  2059.   if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
  2060.     return false;
  2061.   reloc_base = obj_relocbase(abfd);
  2062.     
  2063.   /* Make a pass through the symbol table to count line number entries and
  2064.      put them into the correct asections */
  2065.     
  2066. #ifndef NO_COFF_LINENOS
  2067.   coff_count_linenumbers(abfd);
  2068. #endif
  2069.   data_base = scn_base;
  2070.     
  2071.   /* Work out the size of the reloc and linno areas */
  2072.     
  2073.   for (current = abfd->sections; current != NULL; current = current->next) {
  2074.     reloc_size += current->reloc_count * RELSZ;
  2075. #ifndef NO_COFF_LINENOS
  2076.     lnno_size += current->lineno_count * LINESZ;
  2077. #endif
  2078.     data_base += SCNHSZ;
  2079.   }
  2080.     
  2081.   lineno_base = reloc_base + reloc_size;
  2082.   sym_base = lineno_base + lnno_size;
  2083.     
  2084.   /* Indicate in each section->line_filepos its actual file address */
  2085.   for (current = abfd->sections; current != NULL; current = current->next) {
  2086.     if (current->lineno_count) {
  2087.       current->line_filepos = lineno_base;
  2088.       current->moving_line_filepos = lineno_base;
  2089. #ifndef NO_COFF_LINENOS
  2090.       lineno_base += current->lineno_count * LINESZ;
  2091. #endif
  2092.     }
  2093.     else {
  2094.       current->line_filepos = 0;
  2095.     }
  2096.     if (current->reloc_count) {
  2097.       current->rel_filepos = reloc_base;
  2098.       reloc_base += current->reloc_count * sizeof(struct internal_reloc);
  2099.     }
  2100.     else {
  2101.       current->rel_filepos = 0;
  2102.     }
  2103.   }
  2104.     
  2105.   /* Write section headers to the file.  */
  2106.     
  2107.   bfd_seek(abfd,
  2108.        (file_ptr) ((abfd->flags & EXEC_P) ?
  2109.                (FILHSZ + AOUTSZ) : FILHSZ),
  2110.        SEEK_SET);
  2111.     
  2112.     {
  2113. #if 0
  2114.       unsigned int    pad = abfd->flags & D_PAGED ? data_base : 0;
  2115. #endif
  2116.       unsigned int    pad = 0;
  2117.     
  2118.       for (current = abfd->sections; current != NULL; current = current->next) {
  2119.     struct internal_scnhdr section;
  2120.     strncpy(&(section.s_name[0]), current->name, 8);
  2121.     section.s_vaddr = current->vma + pad;
  2122.     section.s_paddr = current->vma + pad;
  2123.     section.s_size = current->size - pad;
  2124.     /*
  2125.       If this section has no size or is unloadable then the scnptr
  2126.       will be 0 too
  2127.       */
  2128.     if (current->size - pad == 0 ||
  2129.         (current->flags & SEC_LOAD) == 0) {
  2130.       section.s_scnptr = 0;
  2131.         
  2132.     }
  2133.     else {
  2134.       section.s_scnptr = current->filepos;
  2135.     }
  2136.     section.s_relptr = current->rel_filepos;
  2137.     section.s_lnnoptr = current->line_filepos;
  2138.     section.s_nreloc = current->reloc_count;
  2139.     section.s_nlnno = current->lineno_count;
  2140.     if (current->reloc_count != 0)
  2141.       hasrelocs = true;
  2142.     if (current->lineno_count != 0)
  2143.       haslinno = true;
  2144.       
  2145.     section.s_flags = sec_to_styp_flags(current->name,current->flags);
  2146.  
  2147.     if (!strcmp(current->name, _TEXT)) {
  2148.       text_sec = current;
  2149.     } else if (!strcmp(current->name, _DATA)) {
  2150.       data_sec = current;
  2151.     } else if (!strcmp(current->name, _BSS)) {
  2152.       bss_sec = current;
  2153.     } 
  2154.       
  2155. #ifdef I960
  2156.     section.s_align = (current->alignment_power
  2157.                ? 1 << current->alignment_power
  2158.                : 0);
  2159.  
  2160. #endif
  2161.       {
  2162.         SCNHDR          buff;
  2163.  
  2164.         coff_swap_scnhdr_out(abfd, §ion, &buff);
  2165.         bfd_write((PTR) (&buff), 1, SCNHSZ, abfd);
  2166.  
  2167.       }
  2168.     pad = 0;
  2169.       }
  2170.     }
  2171.  
  2172.   /* OK, now set up the filehdr... */
  2173.   internal_f.f_nscns = abfd->section_count;
  2174.   /*
  2175.     We will NOT put a fucking timestamp in the header here. Every time you
  2176.     put it back, I will come in and take it out again. I'm sorry. This
  2177.     field does not belong here.  We fill it with a 0 so it compares the
  2178.     same but is not a reasonable time. -- gnu@cygnus.com
  2179.     */
  2180.   /*
  2181.     Well, I like it, so I'm conditionally compiling it in.
  2182.     steve@cygnus.com
  2183.     */
  2184. #ifdef COFF_TIMESTAMP
  2185.   internal_f.f_timdat = time(0);
  2186. #else
  2187.   internal_f.f_timdat = 0;
  2188. #endif
  2189.  
  2190.   if (bfd_get_symcount(abfd) != 0)
  2191.     internal_f.f_symptr = sym_base;
  2192.   else
  2193.     internal_f.f_symptr = 0;
  2194.  
  2195.   internal_f.f_flags = 0;
  2196.  
  2197.   if (abfd->flags & EXEC_P)
  2198.     internal_f.f_opthdr = AOUTSZ;
  2199.   else
  2200.     internal_f.f_opthdr = 0;
  2201.  
  2202.   if (!hasrelocs)
  2203.     internal_f.f_flags |= F_RELFLG;
  2204.   if (!haslinno)
  2205.     internal_f.f_flags |= F_LNNO;
  2206.   if (0 == bfd_get_symcount(abfd))
  2207.     internal_f.f_flags |= F_LSYMS;
  2208.   if (abfd->flags & EXEC_P)
  2209.     internal_f.f_flags |= F_EXEC;
  2210. #if M88
  2211.   internal_f.f_flags |= F_AR32W;
  2212. #else
  2213.   if (!abfd->xvec->byteorder_big_p)
  2214.     internal_f.f_flags |= F_AR32WR;
  2215. #endif
  2216.   /*
  2217.     FIXME, should do something about the other byte orders and
  2218.     architectures.
  2219.     */
  2220.  
  2221.   /* Set up architecture-dependent stuff */
  2222.  
  2223.     { unsigned int   magic = 0;
  2224.       unsigned short    flags = 0;
  2225.       coff_set_flags(abfd, &magic, &flags);
  2226.       internal_f.f_magic = magic;
  2227.       internal_f.f_flags |= flags;
  2228.       /* ...and the "opt"hdr... */
  2229.  
  2230. #ifdef A29K 
  2231. # ifdef ULTRA3    /* NYU's machine */
  2232.     /* FIXME: This is a bogus check.  I really want to see if there
  2233.      * is a .shbss or a .shdata section, if so then set the magic
  2234.       * number to indicate a shared data executable.
  2235.      */ 
  2236.       if (internal_f.f_nscns >= 7)
  2237.           internal_a.magic = SHMAGIC;     /* Shared magic */
  2238.       else
  2239. # endif /* ULTRA3 */
  2240.           internal_a.magic = NMAGIC;     /* Assume separate i/d */
  2241. #define __A_MAGIC_SET__
  2242. #endif    /* A29K */
  2243. #ifdef I960
  2244.       internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC); 
  2245. #define __A_MAGIC_SET__
  2246. #endif     /* I960 */
  2247. #if M88
  2248. #define __A_MAGIC_SET__
  2249.       internal_a.magic = PAGEMAGICBCS;
  2250. #endif    /* M88 */
  2251.  
  2252. #if M68 || I386 || MIPS
  2253. #define __A_MAGIC_SET__
  2254.      /* Never was anything here for the 68k */ 
  2255. #endif    /* M88 */
  2256.  
  2257. #ifndef __A_MAGIC_SET__
  2258. # include "Your aouthdr magic number is not being set!"
  2259. #else
  2260. # undef __A_MAGIC_SET__
  2261. #endif
  2262.     }
  2263.   /* Now should write relocs, strings, syms */
  2264.   obj_sym_filepos(abfd) = sym_base;
  2265.  
  2266. #ifndef NO_COFF_SYMBOLS
  2267.   if (bfd_get_symcount(abfd) != 0) {
  2268.     coff_renumber_symbols(abfd);
  2269.     coff_mangle_symbols(abfd);
  2270.     coff_write_symbols(abfd);
  2271.     coff_write_linenumbers(abfd);
  2272.     coff_write_relocs(abfd);
  2273.   }
  2274. #endif /* NO_COFF_SYMBOLS */
  2275.   if (text_sec) {
  2276.     internal_a.tsize = text_sec->size;
  2277.     internal_a.text_start =text_sec->size ? text_sec->vma : 0;
  2278.   }
  2279.   if (data_sec) {
  2280.     internal_a.dsize = data_sec->size;
  2281.     internal_a.data_start = data_sec->size ? data_sec->vma      : 0;
  2282.   }
  2283.   if (bss_sec) {
  2284.     internal_a.bsize =  bss_sec->size;
  2285.   }
  2286.  
  2287.   internal_a.entry = bfd_get_start_address(abfd);
  2288.   internal_f.f_nsyms =  bfd_get_symcount(abfd);
  2289.  
  2290.   /* now write them */
  2291.   if (bfd_seek(abfd, 0L, SEEK_SET) != 0)
  2292.     return false;
  2293.     {
  2294.       FILHDR buff;
  2295.       coff_swap_filehdr_out(abfd, &internal_f, &buff);
  2296.       bfd_write((PTR) &buff, 1, FILHSZ, abfd);
  2297.     }
  2298.   if (abfd->flags & EXEC_P) {
  2299.     AOUTHDR buff;
  2300.     coff_swap_aouthdr_out(abfd, &internal_a, &buff);
  2301.     bfd_write((PTR) &buff, 1, AOUTSZ, abfd);
  2302.   }
  2303.   return true;
  2304. }
  2305.  
  2306. #ifndef NO_COFF_SYMBOLS
  2307.  
  2308. /*
  2309. this function transforms the offsets into the symbol table into
  2310. pointers to syments.
  2311. */
  2312.  
  2313.  
  2314. static void
  2315. DEFUN(coff_pointerize_aux,(ignore_abfd, table_base, type, class, auxent),
  2316. bfd *ignore_abfd AND
  2317. combined_entry_type *table_base AND
  2318. int type AND
  2319. int class AND
  2320. combined_entry_type *auxent)
  2321. {
  2322.   /* Don't bother if this is a file or a section */
  2323.   if (class == C_STAT && type == T_NULL) return;
  2324.   if (class == C_FILE) return;
  2325.  
  2326.   /* Otherwise patch up */
  2327.   if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
  2328.     auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
  2329.       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
  2330.     auxent->fix_end = 1;
  2331.   }
  2332.   if (auxent->u.auxent.x_sym.x_tagndx.l != 0) {
  2333.     auxent->u.auxent.x_sym.x_tagndx.p = table_base +  auxent->u.auxent.x_sym.x_tagndx.l;
  2334.     auxent->fix_tag = 1;
  2335.   }
  2336. }
  2337.  
  2338. #endif /* NO_COFF_SYMBOLS */
  2339.  
  2340. static          boolean
  2341. DEFUN(coff_set_section_contents,(abfd, section, location, offset, count),
  2342.       bfd            *abfd AND
  2343.       sec_ptr         section AND
  2344.       PTR             location AND
  2345.       file_ptr        offset AND
  2346.       bfd_size_type   count)
  2347. {
  2348.     if (abfd->output_has_begun == false)    /* set by bfd.c handler */
  2349.     coff_compute_section_file_positions(abfd);
  2350.  
  2351.     bfd_seek(abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
  2352.  
  2353.     if (count != 0) {
  2354.     return (bfd_write(location, 1, count, abfd) == count) ? true : false;
  2355.     }
  2356.     return true;
  2357. }
  2358. #if 0
  2359. static          boolean
  2360. coff_close_and_cleanup(abfd)
  2361.     bfd            *abfd;
  2362. {
  2363.   if (!bfd_read_p(abfd))
  2364.     switch (abfd->format) {
  2365.     case bfd_archive:
  2366.       if (!_bfd_write_archive_contents(abfd))
  2367.     return false;
  2368.       break;
  2369.     case bfd_object:
  2370.       if (!coff_write_object_contents(abfd))
  2371.     return false;
  2372.       break;
  2373.     default:
  2374.       bfd_error = invalid_operation;
  2375.       return false;
  2376.     }
  2377.  
  2378.   /* We depend on bfd_close to free all the memory on the obstack.  */
  2379.   /* FIXME if bfd_release is not using obstacks! */
  2380.   return true;
  2381. }
  2382.  
  2383. #endif
  2384. static PTR 
  2385. buy_and_read(abfd, where, seek_direction, size)
  2386.     bfd            *abfd;
  2387.     file_ptr        where;
  2388.     int             seek_direction;
  2389.     size_t          size;
  2390. {
  2391.     PTR             area = (PTR) bfd_alloc(abfd, size);
  2392.     if (!area) {
  2393.     bfd_error = no_memory;
  2394.     return (NULL);
  2395.     }
  2396.     bfd_seek(abfd, where, seek_direction);
  2397.     if (bfd_read(area, 1, size, abfd) != size) {
  2398.     bfd_error = system_call_error;
  2399.     return (NULL);
  2400.     }                /* on error */
  2401.     return (area);
  2402. }                /* buy_and_read() */
  2403.  
  2404.  
  2405. #ifndef NO_COFF_SYMBOLS
  2406.  
  2407. static char *
  2408. DEFUN(build_string_table,(abfd),
  2409. bfd *abfd)
  2410. {
  2411.   char string_table_size_buffer[4];
  2412.   unsigned int string_table_size;
  2413.   char *string_table;
  2414.   /*
  2415.     At this point we should be "seek"'d to the end of the
  2416.     symbols === the symbol table size.
  2417.     */
  2418.       
  2419.   if (bfd_read((char *) string_table_size_buffer,
  2420.            sizeof(string_table_size_buffer),
  2421.            1, abfd) != sizeof(string_table_size)) {
  2422.     bfd_error = system_call_error;
  2423.     return (NULL);
  2424.   }                /* on error */
  2425.       
  2426.   string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
  2427.       
  2428.   if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
  2429.     bfd_error = no_memory;
  2430.     return (NULL);
  2431.   }                /* on mallocation error */
  2432.   if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
  2433.     bfd_error = system_call_error;
  2434.     return (NULL);
  2435.   }            
  2436.   return string_table;
  2437. }
  2438.  
  2439. /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
  2440.  \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
  2441.  be \0-terminated.  */
  2442. static char *
  2443. DEFUN(copy_name,(abfd, name, maxlen),
  2444.       bfd *abfd AND
  2445.       char *name AND
  2446.       int maxlen)
  2447. {
  2448.   int  len;
  2449.   char *newname;
  2450.  
  2451.   for (len = 0; len < maxlen; ++len) {
  2452.     if (name[len] == '\0') {
  2453.       break;
  2454.     }
  2455.   }
  2456.  
  2457.   if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
  2458.     bfd_error = no_memory;
  2459.     return (NULL);
  2460.   }
  2461.   strncpy(newname, name, len);
  2462.   newname[len] = '\0';
  2463.   return newname;
  2464. }
  2465.  
  2466.  
  2467. /*
  2468. read a symbol table into freshly mallocated memory, swap it, and knit the
  2469. symbol names into a normalized form. By normalized here I mean that all
  2470. symbols have an n_offset pointer that points to a NULL terminated string.
  2471. Oh, and the first symbol MUST be a C_FILE.  If there wasn't one there
  2472. before, put one there.
  2473. */
  2474.  
  2475. static combined_entry_type *
  2476. DEFUN(get_normalized_symtab,(abfd),
  2477. bfd            *abfd)
  2478. {
  2479.  
  2480.   combined_entry_type          *internal;
  2481.   combined_entry_type          *internal_ptr;
  2482.   combined_entry_type         *internal_end;
  2483.   SYMENT *raw;
  2484.   SYMENT *raw_src;
  2485.   SYMENT *raw_end;
  2486.   char           *string_table = NULL;
  2487.   unsigned long   size;
  2488.  
  2489.  
  2490.   unsigned int raw_size;
  2491.   if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
  2492.     return obj_raw_syments(abfd);
  2493.   }
  2494.   if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
  2495.     bfd_error = no_symbols;
  2496.     return (NULL);
  2497.   }
  2498.  
  2499.   internal = (combined_entry_type *)bfd_alloc(abfd, size);
  2500.   internal_end = internal + bfd_get_symcount(abfd);
  2501.  
  2502.   raw_size =      bfd_get_symcount(abfd) * SYMESZ;
  2503.   raw = (SYMENT *)bfd_alloc(abfd,raw_size);
  2504.  
  2505.   if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
  2506.       || bfd_read((PTR)raw, raw_size, 1, abfd) != raw_size) {
  2507.     bfd_error = system_call_error;
  2508.     return (NULL);
  2509.   }
  2510.   /* mark the end of the symbols */
  2511.   raw_end = raw + bfd_get_symcount(abfd);
  2512.   /*
  2513.     FIXME SOMEDAY.  A string table size of zero is very weird, but
  2514.     probably possible.  If one shows up, it will probably kill us.
  2515.     */
  2516.  
  2517.   /* Swap all the raw entries */
  2518.   for (raw_src = raw, internal_ptr = internal; raw_src < raw_end; raw_src++, internal_ptr++) {
  2519.     unsigned int i;
  2520.     coff_swap_sym_in(abfd, (char *)raw_src, (char *)&internal_ptr->u.syment);    
  2521.     internal_ptr->fix_tag = 0;
  2522.     internal_ptr->fix_end = 0;
  2523.  
  2524.     for (i = internal_ptr->u.syment.n_numaux; i; --i, raw_src++, internal_ptr++) {
  2525.       (internal_ptr+1)->fix_tag = 0;
  2526.       (internal_ptr+1)->fix_end = 0;
  2527.  
  2528.       coff_swap_aux_in(abfd, (char *)(raw_src +1), internal_ptr->u.syment.n_type,
  2529.                internal_ptr->u.syment.n_sclass, &     (internal_ptr+1)->u.auxent);
  2530.  
  2531.       coff_pointerize_aux(abfd, 
  2532.               internal,
  2533.               internal_ptr->u.syment.n_type,
  2534.               internal_ptr->u.syment.n_sclass,
  2535.               internal_ptr +1);
  2536.     }
  2537.   }
  2538.       
  2539.   /* Free all the raw stuff */
  2540.   bfd_release(abfd, raw);
  2541.  
  2542.   for (internal_ptr = internal; internal_ptr < internal_end;
  2543.        internal_ptr ++) 
  2544.       {
  2545.     if (internal_ptr->u.syment.n_sclass == C_FILE) {
  2546.       /* make a file symbol point to the name in the auxent, since
  2547.          the text ".file" is redundant */
  2548.       if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
  2549.         /* the filename is a long one, point into the string table
  2550.          */
  2551.         if (string_table == NULL) {
  2552.           string_table = build_string_table(abfd);
  2553.         }
  2554.  
  2555.         internal_ptr->u.syment._n._n_n._n_offset =
  2556.           (int) (string_table - 4 +
  2557.              (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
  2558.       }
  2559.       else {
  2560.         /* ordinary short filename, put into memory anyway */
  2561.         internal_ptr->u.syment._n._n_n._n_offset = (int)
  2562.           copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname, FILNMLEN);
  2563.         
  2564.       }
  2565.     }
  2566.     else {
  2567.       if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
  2568.         /*
  2569.           This is a "short" name.  Make it long.
  2570.           */
  2571.         unsigned long   i = 0;
  2572.         char           *newstring = NULL;
  2573.         /*
  2574.           find the length of this string without walking into memory
  2575.           that isn't ours.
  2576.           */
  2577.     
  2578.         for (i = 0; i < 8; ++i) {
  2579.           if (internal_ptr->u.syment._n._n_name[i] == '\0') {
  2580.         break;
  2581.           }            /* if end of string */
  2582.         }            /* possible lengths of this string. */
  2583.     
  2584.         if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
  2585.           bfd_error = no_memory;
  2586.           return (NULL);
  2587.         }            /* on error */
  2588.         bzero(newstring, i);
  2589.         strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
  2590.         internal_ptr->u.syment._n._n_n._n_offset =  (int) newstring;
  2591.         internal_ptr->u.syment._n._n_n._n_zeroes = 0;
  2592.     
  2593.       }
  2594.       else {
  2595.         /*  This is a long name already. Just point it at the string in memory.   */
  2596.         if (string_table == NULL) {
  2597.           string_table = build_string_table(abfd);
  2598.         }
  2599.         internal_ptr->u.syment._n._n_n._n_offset =
  2600.           (int) (string_table - 4 +  internal_ptr->u.syment._n._n_n._n_offset);
  2601.       }    
  2602.     }
  2603.     internal_ptr += internal_ptr->u.syment.n_numaux;
  2604.       }    
  2605.  
  2606.   obj_raw_syments(abfd) = internal;
  2607.   obj_string_table(abfd) = string_table;
  2608.     
  2609.   return (internal);
  2610. }                /* get_normalized_symtab() */
  2611.  
  2612. #endif /* NO_COFF_SYMBOLS */
  2613.  
  2614. static
  2615. struct sec *
  2616. DEFUN(section_from_bfd_index,(abfd, index),
  2617.       bfd            *abfd AND
  2618.       int             index)
  2619. {
  2620.   if (index > 0) {
  2621.     struct sec *answer = abfd->sections;
  2622.     while (--index) {
  2623.       answer = answer->next;
  2624.     }
  2625.     return answer;
  2626.   }
  2627.   return 0;
  2628. }
  2629.  
  2630. #ifndef NO_COFF_LINENOS
  2631.  
  2632. /*doc*
  2633. @subsubsection Reading Linenumbers
  2634. Createing the linenumber table is done by reading in the entire coff
  2635. linenumber table, and creating another table for internal use.
  2636.  
  2637. A coff line number table is structured so that each
  2638. function is marked as having a line number of 0. Each line within the
  2639. function is an offset from the first line in the function. The base of
  2640. the line number information for the table is stored in the symbol
  2641. associated with the function.
  2642.  
  2643. The information is copied from the external to the internal table, and
  2644. each symbol which marks a function is marked by pointing its...
  2645.  
  2646. **How does this work ?**
  2647.  
  2648. */
  2649.  
  2650. static boolean
  2651. coff_slurp_line_table(abfd, asect)
  2652. bfd            *abfd;
  2653. asection       *asect;
  2654.   {
  2655.     LINENO  *native_lineno;
  2656.     alent          *lineno_cache;
  2657.     
  2658.     BFD_ASSERT(asect->lineno == (alent *) NULL);
  2659.     
  2660.     native_lineno = (LINENO *) buy_and_read(abfd,
  2661.                         asect->line_filepos,
  2662.                         SEEK_SET,
  2663.                         (size_t) (LINESZ *
  2664.                               asect->lineno_count));
  2665.     lineno_cache =
  2666.       (alent *) bfd_alloc(abfd, (size_t) ((asect->lineno_count + 1) * sizeof(alent)));
  2667.     if (lineno_cache == NULL) {
  2668.       bfd_error = no_memory;
  2669.       return false;
  2670.     } else {    
  2671.       unsigned int    counter = 0;
  2672.       alent          *cache_ptr = lineno_cache;
  2673.       LINENO  *src = native_lineno;
  2674.       
  2675.       while (counter < asect->lineno_count) {
  2676.     struct internal_lineno dst;
  2677.     coff_swap_lineno_in(abfd, src, &dst);
  2678.     cache_ptr->line_number = dst.l_lnno;
  2679.     
  2680.     if (cache_ptr->line_number == 0) {
  2681.       coff_symbol_type *sym =
  2682.         (coff_symbol_type *) (dst.l_addr.l_symndx
  2683.                   + obj_symbol_slew(abfd)
  2684.                   + obj_raw_syments(abfd))->u.syment._n._n_n._n_zeroes;
  2685.       cache_ptr->u.sym = (asymbol *) sym;
  2686.       sym->lineno = cache_ptr;
  2687.     }
  2688.     else {
  2689.       cache_ptr->u.offset = dst.l_addr.l_paddr
  2690.         - bfd_section_vma(abfd, asect);
  2691.     }                /* If no linenumber expect a symbol index */
  2692.     
  2693.     cache_ptr++;
  2694.     src++;
  2695.     counter++;
  2696.       }
  2697.       cache_ptr->line_number = 0;
  2698.       
  2699.     }
  2700.     asect->lineno = lineno_cache;
  2701.     /* FIXME, free native_lineno here, or use alloca or something. */
  2702.     return true;
  2703.   }                /* coff_slurp_line_table() */
  2704.  
  2705. #endif /* NO_COFF_LINENOS */
  2706.  
  2707. #ifndef NO_COFF_LINENOS
  2708.  
  2709. static          boolean
  2710. DEFUN(coff_slurp_symbol_table,(abfd),
  2711.       bfd            *abfd)
  2712. {
  2713.   combined_entry_type         *native_symbols;
  2714.   coff_symbol_type *cached_area;
  2715.   unsigned int   *table_ptr;
  2716.     
  2717.   unsigned int    number_of_symbols = 0;
  2718.   if (obj_symbols(abfd))
  2719.     return true;
  2720.   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
  2721.     
  2722.   /* Read in the symbol table */
  2723.   if ((native_symbols = get_normalized_symtab(abfd)) == NULL) {
  2724.     return (false);
  2725.   }                /* on error */
  2726.     
  2727.     
  2728.   /* Allocate enough room for all the symbols in cached form */
  2729.   cached_area =
  2730.     (coff_symbol_type *)
  2731.       bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(coff_symbol_type)));
  2732.     
  2733.   if (cached_area == NULL) {
  2734.     bfd_error = no_memory;
  2735.     return false;
  2736.   }                /* on error */
  2737.   table_ptr =
  2738.     (unsigned int *)
  2739.       bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(unsigned int)));
  2740.     
  2741.   if (table_ptr == NULL) {
  2742.     bfd_error = no_memory;
  2743.     return false;
  2744.   } else {
  2745.     coff_symbol_type *dst = cached_area;
  2746.     unsigned int    last_native_index = bfd_get_symcount(abfd);
  2747.     unsigned int    this_index = 0;
  2748.     while (this_index < last_native_index) {
  2749.       combined_entry_type         *src = native_symbols + this_index;
  2750.       table_ptr[this_index] = number_of_symbols;
  2751.       dst->symbol.the_bfd = abfd;
  2752.     
  2753.       dst->symbol.name = (char *)(src->u.syment._n._n_n._n_offset);
  2754.       /*
  2755.     We use the native name field to point to the cached field
  2756.     */
  2757.       src->u.syment._n._n_n._n_zeroes = (int) dst;
  2758.       dst->symbol.section = section_from_bfd_index(abfd,
  2759.                            src->u.syment.n_scnum);
  2760.       switch (src->u.syment.n_sclass) {
  2761. #ifdef I960
  2762.       case C_LEAFEXT:
  2763. #if 0
  2764.     dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
  2765.     dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
  2766.     dst->symbol.flags |= BSF_NOT_AT_END;
  2767. #endif
  2768.     /* Fall through to next case */
  2769.       
  2770. #endif
  2771.       
  2772.       case C_EXT:
  2773.     if ((src->u.syment.n_scnum) == 0) {
  2774.       if ((src->u.syment.n_value) == 0) {
  2775.         dst->symbol.flags = BSF_UNDEFINED;
  2776.         dst->symbol.value= 0;
  2777.       }
  2778.       else {
  2779.         dst->symbol.flags = BSF_FORT_COMM;
  2780.         dst->symbol.value = (src->u.syment.n_value);
  2781.       }
  2782.     }
  2783.     else {
  2784.       /*
  2785.         Base the value as an index from the base of the
  2786.         section
  2787.         */
  2788.       if (dst->symbol.section == (asection *) NULL) {
  2789.         dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL | BSF_ABSOLUTE;
  2790.         dst->symbol.value = src->u.syment.n_value;
  2791.       }
  2792.       else {
  2793.         dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
  2794.         dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
  2795.       }
  2796.       if (ISFCN((src->u.syment.n_type))) {
  2797.         /*
  2798.           A function ext does not go at the end of a file
  2799.           */
  2800.         dst->symbol.flags |= BSF_NOT_AT_END;
  2801.       }
  2802.     }
  2803.       
  2804.     break;
  2805.       case C_STAT:        /* static             */
  2806. #ifdef I960
  2807.       case C_LEAFSTAT:        /* static leaf procedure        */
  2808. #endif
  2809.       case C_LABEL:        /* label             */
  2810.     if (src->u.syment.n_scnum == -2)
  2811.       dst->symbol.flags = BSF_DEBUGGING;
  2812.     else
  2813.       dst->symbol.flags = BSF_LOCAL;
  2814.     /*
  2815.       Base the value as an index from the base of the section, if
  2816.       there is one
  2817.       */
  2818.     if (dst->symbol.section)
  2819.       dst->symbol.value = (src->u.syment.n_value) -
  2820.         dst->symbol.section->vma;
  2821.     else
  2822.       dst->symbol.value = (src->u.syment.n_value) ;
  2823.     break;
  2824.       
  2825.       case C_MOS:        /* member of structure     */
  2826.       case C_EOS:        /* end of structure         */
  2827. #ifdef NOTDEF    /* C_AUTOARG has the same value */
  2828. #ifdef C_GLBLREG
  2829.       case C_GLBLREG:        /* A29k-specific storage class */
  2830. #endif
  2831. #endif
  2832.       case C_REGPARM:        /* register parameter         */
  2833.       case C_REG:        /* register variable         */
  2834. #ifdef C_AUTOARG
  2835.       case C_AUTOARG:        /* 960-specific storage class */
  2836. #endif
  2837.       case C_TPDEF:        /* type definition         */
  2838.       
  2839.       case C_ARG:
  2840.       case C_AUTO:        /* automatic variable */
  2841.       case C_FIELD:        /* bit field */
  2842.       case C_ENTAG:        /* enumeration tag         */
  2843.       case C_MOE:        /* member of enumeration     */
  2844.       case C_MOU:        /* member of union         */
  2845.       case C_UNTAG:        /* union tag             */
  2846.       
  2847.     dst->symbol.flags = BSF_DEBUGGING;
  2848.     dst->symbol.value = (src->u.syment.n_value);
  2849.     break;
  2850.       
  2851.       case C_FILE:        /* file name             */
  2852.       case C_STRTAG:        /* structure tag         */
  2853.     dst->symbol.flags = BSF_DEBUGGING;
  2854.     dst->symbol.value = (src->u.syment.n_value);
  2855.       
  2856.     break;
  2857.       case C_BLOCK:        /* ".bb" or ".eb"         */
  2858.       case C_FCN:        /* ".bf" or ".ef"         */
  2859.       case C_EFCN:        /* physical end of function     */
  2860.     dst->symbol.flags = BSF_LOCAL;
  2861.     /*
  2862.       Base the value as an index from the base of the section
  2863.       */
  2864.     dst->symbol.value = (src->u.syment.n_value) - dst->symbol.section->vma;
  2865.       
  2866.     break;
  2867.       case C_NULL:
  2868.       case C_EXTDEF:        /* external definition         */
  2869.       case C_ULABEL:        /* undefined label         */
  2870.       case C_USTATIC:        /* undefined static         */
  2871.       case C_LINE:        /* line # reformatted as symbol table entry */
  2872.       case C_ALIAS:        /* duplicate tag         */
  2873.       case C_HIDDEN:        /* ext symbol in dmert public lib */
  2874.       
  2875.       default:
  2876.       
  2877.     fprintf(stderr,"Unrecognized storage class %d\n", 
  2878.                 src->u.syment.n_sclass);
  2879.     abort();
  2880.     dst->symbol.flags = BSF_DEBUGGING;
  2881.     dst->symbol.value = (src->u.syment.n_value);
  2882.       
  2883.     break;
  2884.       }
  2885.     
  2886.       BFD_ASSERT(dst->symbol.flags != 0);
  2887.     
  2888.       dst->native = src;
  2889.     
  2890.       dst->symbol.udata = 0;
  2891.       dst->lineno = (alent *) NULL;
  2892.       this_index += (src->u.syment.n_numaux) + 1;
  2893.       dst++;
  2894.       number_of_symbols++;
  2895.     }                /* walk the native symtab */
  2896.   }                /* bfdize the native symtab */
  2897.     
  2898.   obj_symbols(abfd) = cached_area;
  2899.   obj_raw_syments(abfd) = native_symbols;
  2900.     
  2901.   bfd_get_symcount(abfd) = number_of_symbols;
  2902.   obj_convert(abfd) = table_ptr;
  2903.   /* Slurp the line tables for each section too */
  2904.     {
  2905.       asection       *p;
  2906.       p = abfd->sections;
  2907.       while (p) {
  2908.     coff_slurp_line_table(abfd, p);
  2909.     p = p->next;
  2910.       }
  2911.     }
  2912.   return true;
  2913. }                /* coff_slurp_symbol_table() */
  2914.  
  2915. static unsigned int
  2916. coff_get_symtab_upper_bound(abfd)
  2917. bfd            *abfd;
  2918.   {
  2919.     if (!coff_slurp_symbol_table(abfd))
  2920.       return 0;
  2921.     
  2922.     return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
  2923.   }
  2924.  
  2925.  
  2926. static unsigned int
  2927. coff_get_symtab(abfd, alocation)
  2928. bfd            *abfd;
  2929. asymbol       **alocation;
  2930.   {
  2931.     unsigned int    counter = 0;
  2932.     coff_symbol_type *symbase;
  2933.     coff_symbol_type **location = (coff_symbol_type **) (alocation);
  2934.     if (!coff_slurp_symbol_table(abfd))
  2935.       return 0;
  2936.     
  2937.     for (symbase = obj_symbols(abfd); counter++ < bfd_get_symcount(abfd);)
  2938.       *(location++) = symbase++;
  2939.     *location++ = 0;
  2940.     return bfd_get_symcount(abfd);
  2941.   }
  2942.  
  2943. #endif /* NO_COFF_SYMBOLS */
  2944.  
  2945. static unsigned int
  2946. coff_get_reloc_upper_bound(abfd, asect)
  2947. bfd            *abfd;
  2948. sec_ptr         asect;
  2949.   {
  2950.     if (bfd_get_format(abfd) != bfd_object) {
  2951.       bfd_error = invalid_operation;
  2952.       return 0;
  2953.     }
  2954.     return (asect->reloc_count + 1) * sizeof(arelent *);
  2955.   }
  2956.  
  2957. /*doc*
  2958. @subsubsection Reading Relocations
  2959. Coff relocations are easily transformed into the internal BFD form
  2960. (@code{arelent}). 
  2961.  
  2962. Reading a coff relocation table is done in the following stages:
  2963. @itemize @bullet
  2964. @item 
  2965. The entire coff relocation table is read into memory.
  2966. @item
  2967. Each relocation is processed in turn, first it is swapped from the
  2968. external to the internal form.
  2969. @item
  2970. The symbol referenced in the relocation's symbol index is turned into
  2971. a pointer into the canonical symbol table. Note that this table is the
  2972. same as the one returned by a call to @code{bfd_canonicalize_symtab}.
  2973. The back end will call the routine and save the result if a
  2974. canonicalization hasn't been done.
  2975. @item
  2976. The reloc index is turned into a pointer to a howto structure, in a
  2977. back end specific way. For instance, the 386 and 960 use the
  2978. @code{r_type} to directly produce an index into a howto table vector;
  2979. the 88k subtracts a number from the @code{r_type} field and creates an
  2980. addend field.
  2981. @end itemize
  2982. */
  2983.  
  2984. static          boolean
  2985. DEFUN(coff_slurp_reloc_table,(abfd, asect, symbols),
  2986.       bfd            *abfd AND
  2987.       sec_ptr         asect AND
  2988.       asymbol       **symbols)
  2989.   {
  2990.     RELOC   *native_relocs;
  2991.     arelent        *reloc_cache;
  2992.     if (asect->relocation)
  2993.       return true;
  2994.     if (asect->reloc_count == 0)
  2995.       return true;
  2996. #ifndef NO_COFF_SYMBOLS
  2997.     if (!coff_slurp_symbol_table(abfd))
  2998.       return false;
  2999. #endif
  3000.     native_relocs =
  3001.       (RELOC *) buy_and_read(abfd,
  3002.                  asect->rel_filepos,
  3003.                  SEEK_SET,
  3004.                  (size_t) (RELSZ *
  3005.                        asect->reloc_count));
  3006.     reloc_cache = (arelent *)
  3007.       bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
  3008.     
  3009.     if (reloc_cache == NULL) {
  3010.       bfd_error = no_memory;
  3011.       return false;
  3012.     } {                /* on error */
  3013.       arelent        *cache_ptr;
  3014.       RELOC   *src;
  3015.       for (cache_ptr = reloc_cache,
  3016.        src = native_relocs;
  3017.        cache_ptr < reloc_cache + asect->reloc_count;
  3018.        cache_ptr++,
  3019.        src++) {
  3020.     struct internal_reloc dst;
  3021.     asymbol        *ptr;
  3022.     bfd_swap_reloc_in(abfd, src, &dst);
  3023.     
  3024.     dst.r_symndx += obj_symbol_slew(abfd);
  3025.     cache_ptr->sym_ptr_ptr = symbols + obj_convert(abfd)[dst.r_symndx];
  3026. #ifdef A29K 
  3027.     /* AMD has two relocation entries for the 'consth' instruction.
  3028.      * The first is R_IHIHALF (part 1), the second is R_IHCONST
  3029.      * (part 2).  The second entry's r_symndx does not contain
  3030.      * an index to a symbol but rather a value (apparently).
  3031.      * Also, see the ifdef below for saving the r_symndx value in addend.
  3032.      */
  3033.     if (dst.r_type == R_IHCONST)  {
  3034.         ptr = NULL;
  3035.     } else
  3036. #endif
  3037.     ptr = *(cache_ptr->sym_ptr_ptr);
  3038.     cache_ptr->address = dst.r_vaddr;
  3039.     /*
  3040.       The symbols definitions that we have read in have been
  3041.       relocated as if their sections started at 0. But the offsets
  3042.       refering to the symbols in the raw data have not been
  3043.       modified, so we have to have a negative addend to compensate.
  3044.       
  3045.       Note that symbols which used to be common must be left alone
  3046.       */
  3047.     
  3048.     if (ptr && ptr->the_bfd == abfd 
  3049.         && ptr->section != (asection *) NULL 
  3050.         && ((ptr->flags & BSF_OLD_COMMON)== 0)) 
  3051.         {
  3052. #ifndef M88
  3053.           cache_ptr->addend = -(ptr->section->vma + ptr->value);
  3054. #else
  3055.           cache_ptr->addend = 0;
  3056. #endif
  3057.  
  3058.         }
  3059.     else {
  3060.       cache_ptr->addend = 0;
  3061.     }
  3062.     
  3063.     cache_ptr->address -= asect->vma;
  3064.     
  3065.     cache_ptr->section = (asection *) NULL;
  3066.  
  3067. #ifdef A29K 
  3068.         if (dst.r_type == R_IHCONST) {
  3069.           /* Add in the value which was stored in the symbol index */
  3070.       /* See above comment */
  3071.           cache_ptr->addend += dst.r_symndx;
  3072.           /* Throw away the bogus symbol pointer */
  3073.           cache_ptr->sym_ptr_ptr = 0;
  3074.         }
  3075.     cache_ptr->howto = howto_table + dst.r_type;
  3076. #endif
  3077. #if I386
  3078.     cache_ptr->howto = howto_table + dst.r_type;
  3079. #endif
  3080. #if I960
  3081.     cache_ptr->howto = howto_table + dst.r_type;
  3082. #endif
  3083. #if M68
  3084.     cache_ptr->howto = howto_table + dst.r_type - R_RELBYTE;
  3085. #endif
  3086. #if M88
  3087.     if (dst.r_type >= R_PCR16L && dst.r_type <= R_VRT32) {
  3088.       cache_ptr->howto = howto_table + dst.r_type - R_PCR16L;
  3089.       cache_ptr->addend += dst.r_offset << 16;
  3090.     }
  3091.     else {
  3092.       BFD_ASSERT(0);
  3093.     }
  3094. #endif
  3095.     
  3096.       }
  3097.       
  3098.     }
  3099.     
  3100.     asect->relocation = reloc_cache;
  3101.     return true;
  3102.   }
  3103.  
  3104.  
  3105. /* This is stupid.  This function should be a boolean predicate */
  3106. static unsigned int
  3107. coff_canonicalize_reloc(abfd, section, relptr, symbols)
  3108. bfd            *abfd;
  3109. sec_ptr         section;
  3110. arelent       **relptr;
  3111. asymbol       **symbols;
  3112.   {
  3113.     arelent        *tblptr = section->relocation;
  3114.     unsigned int    count = 0;
  3115.     if (!(tblptr || coff_slurp_reloc_table(abfd, section, symbols)))
  3116.       return 0;
  3117.     tblptr = section->relocation;
  3118.     if (!tblptr)
  3119.       return 0;
  3120.     
  3121.     for (; count++ < section->reloc_count;)
  3122.       *relptr++ = tblptr++;
  3123.     
  3124.     *relptr = 0;
  3125.     
  3126.     return section->reloc_count;
  3127.   }
  3128.  
  3129. #ifndef NO_COFF_SYMBOLS
  3130.  
  3131. /*
  3132. provided a BFD, a section and an offset into the section, calculate and
  3133. return the name of the source file and the line nearest to the wanted
  3134. location.
  3135. */
  3136.  
  3137. static          boolean
  3138. DEFUN(coff_find_nearest_line,(abfd,
  3139.                   section,
  3140.                   ignore_symbols,
  3141.                   offset,
  3142.                   filename_ptr,
  3143.                   functionname_ptr,
  3144.                   line_ptr),
  3145.       bfd            *abfd AND
  3146.       asection       *section AND
  3147.       asymbol       **ignore_symbols AND
  3148.       bfd_vma         offset AND
  3149.       CONST char      **filename_ptr AND
  3150.       CONST char       **functionname_ptr AND
  3151.       unsigned int   *line_ptr)
  3152. {
  3153.   static bfd     *cache_abfd;
  3154.   static asection *cache_section;
  3155.   static bfd_vma  cache_offset;
  3156.   static unsigned int cache_i;
  3157.   static alent   *cache_l;
  3158.     
  3159.   unsigned int    i = 0;
  3160.   coff_data_type *cof = coff_data(abfd);
  3161.   /* Run through the raw syments if available */
  3162.   combined_entry_type *p;
  3163.   alent          *l;
  3164.   unsigned int    line_base = 0;
  3165.     
  3166.     
  3167.   *filename_ptr = 0;
  3168.   *functionname_ptr = 0;
  3169.   *line_ptr = 0;
  3170.     
  3171.   /* Don't try and find line numbers in a non coff file */
  3172.   if (abfd->xvec->flavour != bfd_target_coff_flavour)
  3173.     return false;
  3174.     
  3175.   if (cof == NULL)
  3176.     return false;
  3177.  
  3178.   p = cof->raw_syments;
  3179.     
  3180.   for (i = 0; i < cof->raw_syment_count; i++) {
  3181.     if (p->u.syment.n_sclass == C_FILE) {
  3182.       /* File name has been moved into symbol */
  3183.       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  3184.       break;
  3185.     }
  3186.     p += 1 +  p->u.syment.n_numaux;
  3187.   }
  3188.   /* Now wander though the raw linenumbers of the section */
  3189.   /*
  3190.     If this is the same BFD as we were previously called with and this is
  3191.     the same section, and the offset we want is further down then we can
  3192.     prime the lookup loop
  3193.     */
  3194.   if (abfd == cache_abfd &&
  3195.       section == cache_section &&
  3196.       offset >= cache_offset) {
  3197.     i = cache_i;
  3198.     l = cache_l;
  3199.   }
  3200.   else {
  3201.     i = 0;
  3202.     l = section->lineno;
  3203.   }
  3204.     
  3205.   for (; i < section->lineno_count; i++) {
  3206.     if (l->line_number == 0) {
  3207.       /* Get the symbol this line number points at */
  3208.       coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
  3209.       *functionname_ptr = coff->symbol.name;
  3210.       if (coff->native) {
  3211.     combined_entry_type  *s = coff->native;
  3212.     s = s + 1 + s->u.syment.n_numaux;
  3213.     /*
  3214.       S should now point to the .bf of the function
  3215.       */
  3216.     if (s->u.syment.n_numaux) {
  3217.       /*
  3218.         The linenumber is stored in the auxent
  3219.         */
  3220.       union internal_auxent   *a = &((s + 1)->u.auxent);
  3221.       line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
  3222.     }
  3223.       }
  3224.     }
  3225.     else {
  3226.       if (l->u.offset > offset)
  3227.     break;
  3228.       *line_ptr = l->line_number + line_base + 1;
  3229.     }
  3230.     l++;
  3231.   }
  3232.     
  3233.   cache_abfd = abfd;
  3234.   cache_section = section;
  3235.   cache_offset = offset;
  3236.   cache_i = i;
  3237.   cache_l = l;
  3238.  
  3239.   return true;
  3240. }
  3241.  
  3242. #ifdef GNU960
  3243. file_ptr
  3244. coff_sym_filepos(abfd)
  3245. bfd *abfd;
  3246.   {
  3247.     return obj_sym_filepos(abfd);
  3248.   }
  3249. #endif
  3250.  
  3251. #endif /* NO_COFF_SYMBOLS */
  3252.  
  3253.  
  3254. static int 
  3255. DEFUN(coff_sizeof_headers,(abfd, reloc),
  3256.       bfd *abfd AND
  3257.       boolean reloc)
  3258.   {
  3259.     size_t size;
  3260.     
  3261.     if (reloc == false) {
  3262.       size = FILHSZ + AOUTSZ;
  3263.     }
  3264.     else {
  3265.       size = FILHSZ;
  3266.     }
  3267.     
  3268.     size +=  abfd->section_count * SCNHSZ;
  3269.     return size;
  3270.   }
  3271.  
  3272.  
  3273. #define coff_core_file_failing_command    _bfd_dummy_core_file_failing_command
  3274. #define coff_core_file_failing_signal    _bfd_dummy_core_file_failing_signal
  3275. #define coff_core_file_matches_executable_p    _bfd_dummy_core_file_matches_executable_p
  3276. #define coff_slurp_armap        bfd_slurp_coff_armap
  3277. #define coff_slurp_extended_name_table    _bfd_slurp_extended_name_table
  3278. #define coff_truncate_arname        bfd_dont_truncate_arname
  3279. #define coff_openr_next_archived_file    bfd_generic_openr_next_archived_file
  3280. #define coff_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  3281. #define    coff_get_section_contents    bfd_generic_get_section_contents
  3282. #define    coff_close_and_cleanup        bfd_generic_close_and_cleanup
  3283.  
  3284. #define coff_bfd_debug_info_start        bfd_void
  3285. #define coff_bfd_debug_info_end        bfd_void
  3286. #define coff_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
  3287.  
  3288.